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

Recommended Answers

All 9 Replies

Member Avatar for leegeorg07

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

commented: nice code! +6

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
commented: good idea +6

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

where and on what data you want to use the random, keywords or the txt file?

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']
"""

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.