看看
grouper
功能
itertools recipes
.
import itertools
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 itertools.zip_longest(*args, fillvalue=fillvalue)
from itertools import islice
list =[]
with open('output_of_json.json', 'r') as infile:
lines_gen = grouper(infile, 25, fillvalue='')
for line in lines_gen:
# whatever you want to do
请注意,如果最后一块行少于25行,则该代码将用空行填充25行。