csv.reader length?

John Machin sjmachin at lexicon.net
Fri May 25 19:19:20 EDT 2007


On May 26, 4:49 am, cjl <cjl... at gmail.com> wrote:
> P:
>
> Stupid question:
>
> reader = csv.reader(open('somefile.csv'))
> for row in reader:
>     do something
>
> Any way to determine the "length" of the reader (the number of rows)
> before iterating through the rows?
>
> -CJL

Of course not. A CSV file (even without the embedded newline
complication mentioned by Peter) is a file of variable-length records
separated by a one-or-two-character sequence. Modern Python-supported
filesystems' directories don't keep the information that would be
required to tell you whether a file's records are fixed or variable
length, let alone how many "records" there are in a file.

Why are you asking? Perhaps if you tell us what you are trying to
achieve, we can help you.

Cheers,
John




More information about the Python-list mailing list