I have a file like this:
1
2
3
TAB
1
2
3
TAB
I want to read the lines between TAB as blocks.
import itertools
def block_generator(file):
with open(file) as lines:
for line in lines:
block = list(itertools.takewhile(lambda x: x.rstrip('\n') != '\t',
lines))
yield block
I want to use it as such:
blocks = block_generator(myfile)
for block in blocks:
do_something(block)
The blocks i get all start with the second line like [2,3] [2,3], why?
Here is another approach using groupby