469,903 Members | 2,274 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

csv.reader length?

cjl
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

May 25 '07 #1
5 43607
On May 25, 12:49 pm, cjl <cjl...@gmail.comwrote:
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
How about:

f = open("somefile.csv")
numlines = len(f.readlines())

May 25 '07 #2
7stud wrote:
On May 25, 12:49 pm, cjl <cjl...@gmail.comwrote:
>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?
No. You have to read the records to know the length:

rows = list(reader)
print len(rows)
for row in rows:
# ...
How about:

f = open("somefile.csv")
numlines = len(f.readlines())
No, a field in a csv file may contain newlines:
>>import csv
csv.writer(open("tmp.csv", "w")).writerows([["a", "b\nc"], ["d", "e"]])
len(open("tmp.csv").readlines())
3 # number of lines
>>len(list(csv.reader(open("tmp.csv"))))
2 # number of records

Peter

May 25 '07 #3
Peter Otten wrote:
7stud wrote:
>On May 25, 12:49 pm, cjl <cjl...@gmail.comwrote:
>>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?

No. You have to read the records to know the length:

rows = list(reader)
print len(rows)
for row in rows:
# ...
>How about:

f = open("somefile.csv")
numlines = len(f.readlines())

No, a field in a csv file may contain newlines:
>>>import csv
csv.writer(open("tmp.csv", "w")).writerows([["a", "b\nc"], ["d", "e"]])
len(open("tmp.csv").readlines())
3 # number of lines
>>>len(list(csv.reader(open("tmp.csv"))))
2 # number of records

Peter
Did you try:

import crystal_ball

num_lines=crystal_ball(reader)

Sorry I couldn't resist.

-Larry
May 25 '07 #4
On May 26, 4:49 am, cjl <cjl...@gmail.comwrote:
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

May 25 '07 #5
Larry Bates wrote:
Did you try:

import crystal_ball

num_lines=crystal_ball(reader)
Yes. The answer was a little more comprehensive than I had asked for.
>>print num_lines
42

Peter

May 26 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Ben Baker | last post: by
8 posts views Thread by Stephan | last post: by
8 posts views Thread by Todd Bright | last post: by
1 post views Thread by Aaron | last post: by
5 posts views Thread by Serdar C. | last post: by
2 posts views Thread by lmnorms1 | last post: by
reply views Thread by =?Utf-8?B?QWxoYW1icmEgRWlkb3MgRGVzYXJyb2xsbw==?= | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.