Using enumerate to get line-numbers with itertools grouper?

Victor Hooi victorhooi at gmail.com
Wed Sep 2 06:04:36 EDT 2015


I'm using grouper() to iterate over a textfile in groups of lines:

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(fillvalue=fillvalue, *args)

However, I'd also like to know the line-number that I'm up to, for printing out in informational or error messages.

Is there a way to use enumerate with grouper to achieve this?

The below won't work, as enumerate will give me the index of the group, rather than of the lines themselves:

_BATCH_SIZE = 50

    with open(args.input_file, 'r') as f:
        for line_number, chunk in enumerate(grouper(f, _BATCH_SIZE)):
            print(line_number)

I'm thinking I could do something to modify grouper, maybe, but I'm sure there's an easier way?



More information about the Python-list mailing list