CSV without first line?

S

Sebastian Bassi

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()).
 
J

John Machin

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?
 
S

Sebastian Bassi

So you imagine that there is an undocumented feature?

No, I just think that is documented but I am not able to understand
it. Reading the list I've learned several things that are not directly
inferred from documentation (that is not the same as undocumented
feature).
 
M

memracom

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top