I only want to execute this loop rownum
times:
rownum = 1000
with open('outputfile.csv', 'wb') as out:
with open('inputfile.csv', 'rbU') as ifile:
out_writer = csv.writer(out)
for row in read:
out_writer.writerow(row)
Inserting something like for i in range(0, rownum):
doesn't work, but I can't make sense of where to put it, either. How do I say for y in x N times
?
Use itertools.islice
for row in itertools.islice(read, stop = rownum):
out_writer.writerow(row)
if you don't want to import itertools
, you can also use enumerate
and break
:
for rowidx, row in enumerate(read):
if rowidx == MAX_ROWS: break
out_writer.writerow(row)
itertools
is more pythonic, though.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.