471,328 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,328 software developers and data experts.

CSV without first line?

Hi,

In my CSV file, the first line has the name of the variables. So the
data I want to parse resides from line 2 up to the end. Here is what I
do:

import csv
lines=csv.reader(open("MYFILE"))
lines.next() #this is just to avoid the first line
for line in lines:
DATA PARSING

This works fine. But I don't like to do "lines.next()" just to get rid
of the first line. So I wonder if the reader function on the csv
module has something that could let me parse the file from the second
line (w/o doing that lines.next()).
--
Sebastián Bassi (セバスティアン)
Diplomado en Ciencia y Tecnolog*a.
GPG Fingerprint: 9470 0980 620D ABFC BE63 A4A4 A3DE C97D 8422 D43D
Jul 15 '07 #1
2 6701
On Jul 15, 1:30 pm, "Sebastian Bassi" <sba...@clubdelarazon.org>
wrote:
Hi,

In my CSV file, the first line has the name of the variables. So the
data I want to parse resides from line 2 up to the end. Here is what I
do:

import csv
lines=csv.reader(open("MYFILE"))
lines.next() #this is just to avoid the first line
for line in lines:
DATA PARSING

This works fine. But I don't like to do "lines.next()" just to get rid
of the first line. So I wonder if the reader function on the csv
module has something that could let me parse the file from the second
line (w/o doing that lines.next()).
Why do you wonder, instead of looking in the manual?

Jul 15 '07 #2
On 15 Jul, 04:30, "Sebastian Bassi" <sba...@clubdelarazon.orgwrote:
Hi,

In my CSV file, the first line has the name of the variables. So the
data I want to parse resides from line 2 up to the end. Here is what I
do:

import csv
lines=csv.reader(open("MYFILE"))
lines.next() #this is just to avoid the first line
for line in lines:
DATA PARSING

This works fine. But I don't like to do "lines.next()" just to get rid
of the first line. So I wonder if the reader function on the csv
module has something that could let me parse the file from the second
line (w/o doing that lines.next()).
There is nothing better than the way you are doing it now. Your code
is explicit so that everyone reading it can see that you skip exactly
one line at the beginning.

Imagine that one day someone presents you with a CSV file containing
two header rows? You can easily handle this by writing:

lines.next() # and skip the second line too

but any other magic method that you can find, might not work.
Jul 22 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by krystoffff | last post: by
37 posts views Thread by Haines Brown | last post: by
6 posts views Thread by Kamilche | last post: by
17 posts views Thread by sonu | last post: by
reply views Thread by rosydwin | last post: by

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.