473,471 Members | 2,613 Online
Bytes | Software Development & Data Engineering Community
Create 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 46872
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
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
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
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
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
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
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
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
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
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
muto222
php
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.