I have a very big file, like this:
[PATTERN1] line1 line2 line3 ... ... [END PATTERN] [PATTERN2] line1 line2 ... ... [END PATTERN]
I need to extract
Consider:
# hi
# there
# begin
# need
# this
# stuff
# end
# skip
# this
with open(__file__) as fp:
for line in iter(fp.readline, '# begin\n'):
pass
for line in iter(fp.readline, '# end\n'):
print line
prints "need this stuff"
More flexible (e.g. to allow re pattern matching) is to use itertools drop- and takewhile:
with open(__file__) as fp:
result = list(itertools.takewhile(lambda x: 'end' not in x,
itertools.dropwhile(lambda x: 'begin' not in x, fp)))