finding out the number of rows in a CSV file [Resolved]
Peter Otten
__peter__ at web.de
Wed Aug 27 13:35:51 EDT 2008
John S wrote:
> [OP] Jon Clements wrote:
>> On Aug 27, 12:54 pm, SimonPalmer <simon.pal... at gmail.com> wrote:
>>> after reading the file throughthe csv.reader for the length I cannot
>>> iterate over the rows. How do I reset the row iterator?
>
> A CSV file is just a text file. Don't use csv.reader for counting rows
> -- it's overkill. You can just read the file normally, counting lines
> (lines == rows).
Wrong. A field may have embedded newlines:
>>> import csv
>>> csv.writer(open("tmp.csv", "w")).writerow(["a" + "\n"*10 + "b"])
>>> sum(1 for row in csv.reader(open("tmp.csv")))
1
>>> sum(1 for line in open("tmp.csv"))
11
Peter
More information about the Python-list
mailing list