473,503 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 46889
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3472
by: Ben Baker | last post by:
Hi, Is it possible to use javascript to detect if a screen reader is running a on a users machine? I know that on a pc, screen readers use the MSSA interface. Is there a way to detect...
8
27609
by: Stephan | last post by:
I'm fairly new to python and am working on parsing some delimited text files. I noticed that there's a nice CSV reading/writing module included in the libraries. My data files however, are odd...
8
2494
by: Todd Bright | last post by:
Is there a way to get the current XmlNode from the reader while in the validation event handler? What I'd like to do is display the error message along with the name of its parent node. In my...
1
1944
by: Aaron | last post by:
I asked for a script that can read info inside a specific xml tag and someone gave me this example. XmlReader reader = new XmlTextReader( filename ); while ( reader.Read() ) { if (...
5
3252
by: Serdar C. | last post by:
hello everyone, i have a question about retrieving data from a bar code reader plugged in keyboard port (ps/2) i really dont know how to retrieve data from keyboard, i tried some methods but all i...
2
1377
by: lmnorms1 | last post by:
Hello, I am looking to update a record that was read in with OleDbDataReader. I have the record in reader but now with the reader connection open; how do I update it. Any advice would be...
1
4074
by: vbDavidC | last post by:
Hi, I am fairly new to .net and objects. I learned to create a reader object in method 1, however if I wanted to create multiple select queries in the same module I did not know how to reuse...
0
1736
by: ridgedale | last post by:
Does anyone know how I can prevent the Acrobat Reader plugin from loading a pdf into the web browser? I need to force the user to download the pdf file and run the pdf using the full Adobe Acrobat...
0
1808
by: =?Utf-8?B?QWxoYW1icmEgRWlkb3MgRGVzYXJyb2xsbw==?= | last post by:
Hi all people, everybody, We have multiple versions of Acrobat Reader from 5.x to 8.x, I want to create a method in C# or VB.NET to check to see if the registry key for the versions...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7348
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7006
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7467
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5021
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1519
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.