finding out the number of rows in a CSV file [Resolved]

John S jstrickler at gmail.com
Wed Aug 27 13:22:15 EDT 2008


[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).

This is similar to what Jon Clements said, but you don't need the csv
module.

num_rows = sum(1 for line in open("myfile.csv"))

As other posters have said, there is no free lunch. When you use
csv.reader, it reads the lines, so once it's finished you're at the
end of the file.




More information about the Python-list mailing list