Using enumerate to get line-numbers with itertools grouper?

Peter Otten __peter__ at web.de
Wed Sep 2 06:36:25 EDT 2015


Victor Hooi wrote:

> 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?

print(line_number * _BATCH_SIZE)

Eureka ;)




More information about the Python-list mailing list