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?
You can also group the records from one key word to the next.
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