While researching options for an oft-asked question (How to find the number of rows in a CSV reader before iterating), I came across the approach of using len(list(myCSVReader))
. Yes, I know how klunky and potentially expensive this is, and I won't be using it. But while playing with it, I came across a really puzzling inconsistency:
#myFile.txt is a 3-line CSV file
myCSV = csv.reader(open('myFile.txt','rb'),dialect='excel')
print(len(list(myCSV)))
print(list(myCSV))
When I run this, I get:
> 3
> []
I can understand why you couldn't just use list()
to convert the reader to a list of lists, but if that's the case, then how/why does a len()
on this produce the correct result?
-- JDM