Reputation: 834
I am wondering if there is a way in Python 2.7 to read through a text document to look for certain words, and then if it finds those words, it will read a few lines down to look for a new word. If it doesn't find the new word, it will go back to looking for the first word.
For example my text doc looks like this:
1.Section
2. Cheesecake
3. 0
4. 0
5. 0
6. Tastes good
7. 0
8. 0
9. Donut
10. 0
11. Tastes good
12. 0
13. Cheesecake
14. 0
15. 0
16. Tastes bad
This is the code I have so far:
import sys
fileName = open("filename.txt", "r")
while True:
line = fileName.readline()
if line is None: break
else:
if line.startswith('CHEESECAKE'):
print (line)
x = raw_input(" Continue? ")
if x == "n":
sys.exit()
I don't know what to do from this point on! How can I get it to search for cheesecakes and then check if they taste good?
Upvotes: 1
Views: 289
Reputation: 16
You can also group the records from one key word to the next.
import sys
def process_group(list_in):
if len(list_in):
for rec in list_in:
if rec.startswith("Tastes"):
print "Tastes found for", list_in[0]
return
print "Tastes NOT found for", list_in[0]
open_file = open("filename.txt", "r")
group_list=[]
start_list=["CHEESECAKE", "DONUT"]
for line in open_file:
for st in start_list:
if line.upper().startswith(st): ## process this group
print (line)
process_group(group_list)
group_list=[] ## an empty list for the next group
x = raw_input(" Continue? ")
if x == "n":
sys.exit()
group_list.append(line)
process_group(group_list) # process the last group
Upvotes: 0
Reputation: 27575
Using regular expressions:
filecontent = open("filename.txt", "r").read()
results = re.findall('Cheesecake.*?Tastes\s+?(\w*)\s+?', filecontent, re.DOTALL)
print results
The best way to look for patterns in a file is to read it all at once in a single step, except it is a very very long file. Then, you use regular expression with the re.DOTALL flag. It means the newlines are treated as regular characters, allowing the search pattern to span multiple lines.
If you want, you can mix a part of this with your previous code, to allow the user to go one match at a time. Then, you should use re.search(pattern, text, re.DOTALL).group(1)
to get each individual match content.
Upvotes: 0
Reputation: 1435
Try something along these lines....
if line.startswith('CHEESECAKE'):
do something
elseif line.startswith('tastegood'):
do something
else:
do something
print (line)
x = raw_input(" Continue? ")
if x == "n":
sys.exit()
Upvotes: 1
Reputation: 27038
You can step forward in the line iterator with something like thisline=line.next()
you can also continue reading lines within your main loop with something like
for tmp in line:
if tmp eq "bla" :
... do something.
break
Upvotes: 0