An open file is an iterator of lines. You want an iterator of groups of lines.
Since all of these groups are 6 lines long (counting the blank line at the end), the easiest way to do this is to use the grouper
example from the itertools
recipes in the docs. (You can also get a pre-made version from the more-itertools
library on PyPI, if you prefer.)
from itertools import *
def grouper(iterable, n, fillvalue=None):
"Collect data into fixed-length chunks or blocks"
# grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return zip_longest(*args, fillvalue=fillvalue)
with open(path) as f:
for group in grouper(f, 6):
do_something(group)
If the length of your groups isn't known in advance (even if it will always be consistent within a file), you can instead use groupby
to create alternating groups of empty and non-empty lines. This is kind of like using split
on a string.
We can just use bool
as a key function here—a non-empty line is truthy, and an empty line is falsey. (If that seems odd to you, you can write something like lambda line: line
or lambda line: line != ''
instead.)
with open(path) as f:
for nonempty, group in groupby(f, bool):
if nonempty:
do_something(group)
Or, if this seems way over your head… well, first read David Beazley's Generator Tricks for Systems Programmers, and maybe it won't be over your head anymore. But if it is, we can do the same thing a bit more explicitly:
with open(path) as f:
group = []
for line in f:
if line:
group.append(line)
else:
do_something(group)
group = []
if group:
do_something(group)