473,549 Members | 2,588 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

finding out the number of rows in a CSV file

anyone know how I would find out how many rows are in a csv file?

I can't find a method which does this on csv.reader.

Thanks in advance
Aug 27 '08 #1
17 60017
On Aug 27, 12:16 pm, SimonPalmer <simon.pal...@g mail.comwrote:
anyone know how I would find out how many rows are in a csv file?

I can't find a method which does this on csv.reader.

Thanks in advance
You have to iterate each row and count them -- there's no other way
without supporting information (since each row length is naturally
variable, you can't even use the file size as an indicator).

Something like:

row_count = sum(1 for row in csv.reader( open('filename. csv') ) )

hth
Jon.
Aug 27 '08 #2
2008/8/27 SimonPalmer <si**********@g mail.com>:
anyone know how I would find out how many rows are in a csv file?

I can't find a method which does this on csv.reader.
len(list(csv.re ader(open('my.c sv'))))

--
Cheers,
Simon B.
si***@brunningo nline.net
http://www.brunningonline.net/simon/blog/
Aug 27 '08 #3
On Aug 27, 12:29 pm, "Simon Brunning" <si...@brunning online.net>
wrote:
2008/8/27 SimonPalmer <simon.pal...@g mail.com>:
anyone know how I would find out how many rows are in a csv file?
I can't find a method which does this on csv.reader.

len(list(csv.re ader(open('my.c sv'))))

--
Cheers,
Simon B.
si...@brunningo nline.nethttp://www.brunningonl ine.net/simon/blog/
Not the best of ideas if the row size or number of rows is large!
Manufacture a list, then discard to get its length -- ouch!
Aug 27 '08 #4
2008/8/27 Jon Clements <jo****@googlem ail.com>:
>len(list(csv.r eader(open('my. csv'))))
Not the best of ideas if the row size or number of rows is large!
Manufacture a list, then discard to get its length -- ouch!
I do try to avoid premature optimization. ;-)

--
Cheers,
Simon B.
Aug 27 '08 #5
On Aug 27, 12:41 pm, Jon Clements <jon...@googlem ail.comwrote:
On Aug 27, 12:29 pm, "Simon Brunning" <si...@brunning online.net>
wrote:
2008/8/27 SimonPalmer <simon.pal...@g mail.com>:
anyone know how I would find out how many rows are in a csv file?
I can't find a method which does this on csv.reader.
len(list(csv.re ader(open('my.c sv'))))
--
Cheers,
Simon B.
si...@brunningo nline.nethttp://www.brunningonl ine.net/simon/blog/

Not the best of ideas if the row size or number of rows is large!
Manufacture a list, then discard to get its length -- ouch!
Thanks to everyone for their suggestions.

In my case the number of rows is never going to be that large (<200)
so it is a practical if slightly inelegant solution
Aug 27 '08 #6
On Aug 27, 12:50 pm, SimonPalmer <simon.pal...@g mail.comwrote:
On Aug 27, 12:41 pm, Jon Clements <jon...@googlem ail.comwrote:
On Aug 27, 12:29 pm, "Simon Brunning" <si...@brunning online.net>
wrote:
2008/8/27 SimonPalmer <simon.pal...@g mail.com>:
anyone know how I would find out how many rows are in a csv file?
I can't find a method which does this on csv.reader.
len(list(csv.re ader(open('my.c sv'))))
--
Cheers,
Simon B.
si...@brunningo nline.nethttp://www.brunningonl ine.net/simon/blog/
Not the best of ideas if the row size or number of rows is large!
Manufacture a list, then discard to get its length -- ouch!

Thanks to everyone for their suggestions.

In my case the number of rows is never going to be that large (<200)
so it is a practical if slightly inelegant solution
actually not resolved...

after reading the file throughthe csv.reader for the length I cannot
iterate over the rows. How do I reset the row iterator?
Aug 27 '08 #7
On Aug 27, 12:48 pm, "Simon Brunning" <si...@brunning online.net>
wrote:
2008/8/27 Jon Clements <jon...@googlem ail.com>:
len(list(csv.re ader(open('my.c sv'))))
Not the best of ideas if the row size or number of rows is large!
Manufacture a list, then discard to get its length -- ouch!

I do try to avoid premature optimization. ;-)

--
Cheers,
Simon B.
:)
Aug 27 '08 #8
On Aug 27, 12:54 pm, SimonPalmer <simon.pal...@g mail.comwrote:
On Aug 27, 12:50 pm, SimonPalmer <simon.pal...@g mail.comwrote:
On Aug 27, 12:41 pm, Jon Clements <jon...@googlem ail.comwrote:
On Aug 27, 12:29 pm, "Simon Brunning" <si...@brunning online.net>
wrote:
2008/8/27 SimonPalmer <simon.pal...@g mail.com>:
anyone know how I would find out how many rows are in a csv file?
I can't find a method which does this on csv.reader.
len(list(csv.re ader(open('my.c sv'))))
--
Cheers,
Simon B.
si...@brunningo nline.nethttp://www.brunningonl ine.net/simon/blog/
Not the best of ideas if the row size or number of rows is large!
Manufacture a list, then discard to get its length -- ouch!
Thanks to everyone for their suggestions.
In my case the number of rows is never going to be that large (<200)
so it is a practical if slightly inelegant solution

actually not resolved...

after reading the file throughthe csv.reader for the length I cannot
iterate over the rows. How do I reset the row iterator?
If you're sure that the number of rows is always less than 200.

Slightly modify Simon Brunning's example and do:

rows = list( csv.reader(open ('filename.csv' )) )
row_count = len(rows)
for row in rows:
# do something


Aug 27 '08 #9
On Aug 27, 9:54 pm, SimonPalmer <simon.pal...@g mail.comwrote:
On Aug 27, 12:50 pm, SimonPalmer <simon.pal...@g mail.comwrote:
On Aug 27, 12:41 pm, Jon Clements <jon...@googlem ail.comwrote:
On Aug 27, 12:29 pm, "Simon Brunning" <si...@brunning online.net>
wrote:
2008/8/27 SimonPalmer <simon.pal...@g mail.com>:
anyone know how I would find out how many rows are in a csv file?
I can't find a method which does this on csv.reader.
len(list(csv.re ader(open('my.c sv'))))
--
Cheers,
Simon B.
si...@brunningo nline.nethttp://www.brunningonl ine.net/simon/blog/
Not the best of ideas if the row size or number of rows is large!
Manufacture a list, then discard to get its length -- ouch!
Thanks to everyone for their suggestions.
In my case the number of rows is never going to be that large (<200)
so it is a practical if slightly inelegant solution

actually not resolved...

after reading the file throughthe csv.reader for the length I cannot
iterate over the rows.
OK, I'll bite: Why do you think you need to know the number of rows in
advance?
How do I reset the row iterator?
You don't. You throw it away and get another one. You need to seek to
the beginning of the file first. E.g.:

C:\junk>type foo.csv
blah,blah
waffle
q,w,e,r,t,y

C:\junk>type csv2iters.py
import csv
f = open('foo.csv', 'rb')
rdr = csv.reader(f)
n = 0
for row in rdr:
n += 1
print n, f.tell()
f.seek(0)
rdr = csv.reader(f)
for row in rdr:
print row

C:\junk>csv2ite rs.py
3 32
['blah', 'blah']
['waffle']
['q', 'w', 'e', 'r', 't', 'y']

HTH,
John

Aug 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1536
by: esmith2112 | last post by:
I have a situation that I can't explain. Boiled down to its essence, I have a query of the form SELECT A.COL1, A.COL2, B.COL1 FROM A LEFT JOIN B ON A.KEY = B.KEY This query produces 5383 rows of output. Because B.COL1 was defined as VARCHAR(2000), it was making my ouput file too hard to browse so I eliminated it from the select clause.
0
1098
by: Petr Man | last post by:
Hello everyone, I have a repeatedly running process, which always creates a new logfile with an ending n+1. What I need is to find the last file, the one with highest number at the end. The problem is, that the max() method gives me a wrong answer. I tried to convert the items in my list into integers using int(), but that ends up with an...
6
4016
by: rshivaraman | last post by:
CREATE TABLE ( (10) NULL ) CREATE TABLE ( (10) NULL )
2
1182
by: Kururu | last post by:
Hi Does anyone know how to make a form application which can get no. of file in the directory and size??? Thousand Thanks Kururu
1
9371
by: sharadadutt1981 | last post by:
hi all, I was trying to fetch even or odd number rows from database. currently i am usying DB2 ver 8.0. any one can help me out.
11
4903
by: John | last post by:
Is there a way to find the number of processors on a machine (on linux/ windows/macos/cygwin) using python code (using the same code/cross platform code)?
25
4532
by: Daniel Kraft | last post by:
Hi, I do need to implement something similar to C++'s std::bitset in C; for this, I use an array of int's to get together any desired number of bits, possibly larger than 32/64 or anything like this. So of course it does not matter how many bits a single int has, but I do need a reliable way to find it out. I think of something like
1
1829
by: clickingwires | last post by:
How do you consecutively number rows in an aggregate query?
1
2227
by: ranaharis | last post by:
how can i get line number of file where error has occured?
1
2087
by: jeddiki | last post by:
Hi, I want to get a subset from my table that includes rows that have an item (cb_id) with a unique ip address ( ip_adr). To be in the subset there should be at least two rows and if there are more than four rows then the rows above the forth should be filtered out. eg. table contains: EBFORTUNE, IP add: 55.167.889.182 EBFORTUNE,...
0
7532
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
7461
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
7730
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. ...
1
7491
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7823
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5101
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3509
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1956
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
1
1068
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.