473,554 Members | 2,870 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 46910
On May 25, 12:49 pm, cjl <cjl...@gmail.c omwrote:
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.c omwrote:
>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(op en("tmp.csv", "w")).writerows ([["a", "b\nc"], ["d", "e"]])
len(open("tmp .csv").readline s())
3 # number of lines
>>len(list(csv. reader(open("tm p.csv"))))
2 # number of records

Peter

May 25 '07 #3
Peter Otten wrote:
7stud wrote:
>On May 25, 12:49 pm, cjl <cjl...@gmail.c omwrote:
>>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(o pen("tmp.csv", "w")).writerows ([["a", "b\nc"], ["d", "e"]])
len(open("tm p.csv").readlin es())
3 # number of lines
>>>len(list(csv .reader(open("t mp.csv"))))
2 # number of records

Peter
Did you try:

import crystal_ball

num_lines=cryst al_ball(reader)

Sorry I couldn't resist.

-Larry
May 25 '07 #4
On May 26, 4:49 am, cjl <cjl...@gmail.c omwrote:
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=cryst al_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
3481
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 whether this is operational or not? Thanks,
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 in that they are composed of lines with alternating formats. (Essentially the rows are a header record and a corresponding detail record on the next...
8
2507
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 XML format, there are parent nodes named differently with some of the same names for child nodes, so when I get an error message that references the...
1
1949
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 ( reader.NodeType == XmlNodeType.Element ) { string val; switch ( reader.Name )
5
3257
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 could get was getting key values like "F7 key", or "Shift key"... can someone tell me how to retrieve data from a bar code scanner please? or at...
2
1382
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 appreciated. Thank you, lmnorms
1
4081
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 the same code, I would like to verify if method 2 is the right way to reuse the same reader object. If this is the case could I open a reader...
0
1746
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 Reader program. The code I'm using at present isn't working - any browser with the plugin loaded doesn't download the file it gets loaded into the...
0
1810
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 exists, and if so uninstall it, then install the latest
0
7578
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7497
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7780
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8010
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6119
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5414
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3530
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1994
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 we have to send another system
0
812
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.