Suppose I have this data in a text file, the script extracts everything between index1 and index2 and includes those strings in the output file. But for some reason it stops a few lines before index2.
Dumb Data
index1 0000
random data
index1 0000
random data
index1 0000
index2 0000
Here is my code; it starts writing to my output file as soon as it sees index1, but then if it sees index2, it should write that last match and exit. But it never exits, it seems to hang and stop a few lines before index2, always on the same line though. If the data wasn't sensitive I would paste the actual data.
import re
myvar = False
myfile = open('extract','w')
with open('input.txt') as f:
for line in f:
if re.search(r'index1', line):
myvar = True
myfile.write(line)
elif re.search(r'index2', line):
myvar = False
break
elif myvar == True:
myfile.write(line)
continue
myfile.close
f.close
The thing is, it works with my dummy data, but not with the real data, it stops on this line. It starts with a form feed, which I though might be messing it up, but there are multiple form feeds before this one which is printed to the output file.
FF (redacted) whitespace whitespace (redacted) datetime at datetime page 50
Thank you.