Hi im doing a basic script to parse a text file
I want to look for a particular set of words. When i find the word then record the lines until there is a line that is blank

for line in logfile:
    for word in line.split():
        if word in KEYWORDS:
            print word
            while line != '\n':
               print line
               #Move to next line

Any help would be greatly appreciated

all you were doing is having a endless loop, this is what i did with some added features

logfile = open("logfile.txt", "r").readlines()
KEYWORDS = ['test', 'text']
counterline = []
counter = 0
for line in logfile:
    for word in line.split():
        counter+=1
        if word in KEYWORDS:
            counterline.append(counter)
            print word
print KEYWORDS
print counterline

see what you can do with that

Comments
nice code!

If you want to print from the word to the end of the line, use index. This untested code will tell you if a word is found. You can then use .join to print out what you want.

for line in logfile:
    words_list = line.split():
    for word in KEYWORDS:
          ## or for word in words_list:
          ## use the shortest list in the for() statement
          print word
          if word in words_list:
               el_number = words_list.index( word )
               print "found", el_number, word
    print line
    #Move to next line
Comments
good idea

for line in song:
for word in line.split():
counter+=1
if word in KEYWORDS:
counterline.append(counter)
print line,


Hi I tried the code snippets above and they work just great. But from the "found" list I wanted to be able to randomly select a few of them only and print those out.
How would I be able to expand the above codes to do that. I tried using random.sample(line,3) but that did not work for me. Thanks very much

so here is the code
for line in song:
for word in line.split():
counter+=1
if word in KEYWORDS:
counterline.append(counter)
print line,

sorry about that
The code is
KEYWORDS =
counterline = []
counter = 0
for line in song:
for word in line.split()
counter+=1
if word in KEYWORDS:
counterline.append(counter)
x=random.sample(line,10)
print x,
I'm expecting once it found the selection, from that selection it will randomly select 10 of the lines that had the KEYWORDS in them and report them out.

KEYWORDS = ['Blues', 'Bossa']
counterline = []
counter = 0
for line in song:
    for word in line.split()
        counter+=1
        if word in KEYWORDS:
            counterline.append(counter)
            x=random.sample(line,10)
            print x,

I'm expecting once it found the selection, from that selection it will randomly select 10 of the lines that had the KEYWORDS in them and report them out.

Why are you collecting counter numbers in counterline list? Line is string of one line of song, are you sure you want random letters from it?

import random
line = 'Faithfull, faithfull I adore thee'

print random.sample(line,10)
""" Example output:
['e', 'i', 'a', 'f', 'f', 'u', ' ', ',', 'i', 'l']
"""

Edited 5 Years Ago by pyTony: n/a

That code that I had earlier did not work, I know that.
I don't want random letters from it. Once it located the KEYWORD, I want the entire line name not random letters from it.

This article has been dead for over six months. Start a new discussion instead.