I have a small file read routine and I want only the 1st 200 records I have it working but along the way I could not figure out what was wrong with using the “while” construct.
This code works:
import csv, sys, zipfile
sys.argv[0] = "/home/tom/Documents/REdata/AllListing1RES.zip"
zip_file = zipfile.ZipFile(sys.argv[0])
items_file = zip_file.open('AllListing1RES.txt', 'rU')
rows = []
for row_index, row in enumerate(csv.DictReader(items_file, dialect='excel', delimiter='\t')):
if (row_index < 200):
rows.append(row)
else : break
This code runs until it fails with an out of memory condition I would have thought it was equivalent?
import csv, sys, zipfile
sys.argv[0] = "/home/tom/Documents/REdata/AllListing1RES.zip"
zip_file = zipfile.ZipFile(sys.argv[0])
items_file = zip_file.open('AllListing1RES.txt', 'rU')
rows = []
for row_index, row in enumerate(csv.DictReader(items_file, dialect='excel', delimiter='\t')):
while (row_index < 200):
rows.append(row)
else : break
so what would be the right construct using while? –
The more traditional way of writing that loop would be:
As soon as the row counter hits 200, we bail out of the loop.
To use a
whileloop instead of aforloop (note that, as a looping construct,whileis an alternative toforrather than toif) it is necessary to step through the iterator manually:All that said, there’s actually a superior alternative to both of these options available in the
itertoolsmodule: