By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,714 Members | 750 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,714 IT Pros & Developers. It's quick & easy.

csv.reader length?

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.