您的代码已经以19行为一组提取行,所以我不确定您的问题是什么。在
我可以稍微清理一下您的解决方案,但它的作用与您的代码相同:from itertools import izip_longest
# grouping recipe from itertools documentation
def grouper(n, iterable, fillvalue=None):
"Collect data into fixed-length chunks or blocks"
# grouper(3, 'ABCDEFG', 'x') > ABC DEF Gxx
args = [iter(iterable)] * n
return izip_longest(fillvalue=fillvalue, *args)
def process_chunk(chunk):
"Return sequence of result lines. Chunk must be iterable."
for i, line in enumerate(chunk):
yield 'file-line {1:03d}; chunk-line {0:02d}\n'.format(i, int(line))
yield ' \n'
下面是一些测试代码,它演示了每一行都被访问:
^{pr2}$
此测试用例应该打印如下行:file-line 000; chunk-line 00
file-line 001; chunk-line 01
file-line 002; chunk-line 02
file-line 003; chunk-line 03
file-line 004; chunk-line 04
...
file-line 016; chunk-line 16
file-line 017; chunk-line 17
file-line 018; chunk-line 18
file-line 019; chunk-line 00
file-line 020; chunk-line 01
file-line 021; chunk-line 02
...
file-line 186; chunk-line 15
file-line 187; chunk-line 16
file-line 188; chunk-line 17
file-line 189; chunk-line 18