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 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.
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!
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.
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
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?
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.
:)
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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.
|
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...
|
by: rshivaraman |
last post by:
CREATE TABLE (
(10)
NULL
)
CREATE TABLE (
(10)
NULL
)
|
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
|
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.
| |
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)?
|
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
|
by: clickingwires |
last post by:
How do you consecutively number rows in an aggregate query?
|
by: ranaharis |
last post by:
how can i get line number of file where error has occured?
|
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,...
|
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...
| |
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...
|
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. ...
|
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...
|
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...
|
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...
|
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...
| |
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |