FAQ

On 9/2/2015 6:04 AM, 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?

Without a runnable test example, it is hard to be sure what you want.
However, I believe replacing 'iter(iterable)' with 'enumerate(iterable,
1)', and taking into account that you will get (line_number, line)
tuples instead of lines, will do what you want.


--
Terry Jan Reedy

Search Discussions

Discussion Posts

Previous

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 6 of 6 | next ›
Discussion Overview
grouppython-list @
categoriespython
postedSep 2, '15 at 10:04a
activeSep 2, '15 at 5:48p
posts6
users4
websitepython.org

People

Translate

site design / logo © 2017 Grokbase