Looping through File Question

P

planetmatt

I am a Python beginner. I am trying to loop through a CSV file which
I can do. What I want to change though is for the loop to start at
row 2 in the file thus excluding column headers.

At present I am using this statement to initiate a loop though the
records:

for line in f.readlines():

How do I start this at row 2?
 
V

vijayca

I am a Python beginner. I am trying to loop through a CSV file which
I can do. What I want to change though is for the loop to start at
row 2 in the file thus excluding column headers.

At present I am using this statement to initiate a loop though the
records:

for line in f.readlines():

How do I start this at row 2?

just use readline() method...this will move the file pointer to the
next line....
example:
fd=open("/tmp.txt","r")
fd.readline() ##THIS MOVES THE FILE POINTER TO THE
SECOND LINE

now carry on with...
for line in f.readlines(): #THIS WILL BEGIN WITH THE SECOND ROW AS
THE FILE POINTER IS IN 2NDLINE
....
 
A

Amit Khemka

you can simply call (and maybe throw away) f.readline() a single time before
looping.
If the lenght of the first line is fixed, you can also use f.seek to start
reading from the second row.

francesco

Btw, if you are trying to read a csv file and parse it, you can save
some work .. have a look at "csv" module !
 
J

John Machin

I am a Python beginner. I am trying to loop through a CSV file which
I can do. What I want to change though is for the loop to start at
row 2 in the file thus excluding column headers.

At present I am using this statement to initiate a loop though the
records:

for line in f.readlines():

How do I start this at row 2?

The quick answer to your literal question is:
for line in f.readlines()[1:]:
or, with extreme loss of elegance, this:
for lino, line in enumerate(f.readlines()):
if not lino:
continue

But readline and readlines are old hat, and you wouldn't want to read
a file of a few million lines into a big list, so a better answer is:

_unused = f.next()
for line in f:

But you did say you were reading a CSV file, and you don't really want
to do your own CSV parsing, even if you think you know how to get it
right, so best is:

import csv
rdr = csv.reader(f)
heading_row = rdr.next()
for data_row in rdr:

HTH,
John
 
P

planetmatt

I am a Python beginner. I am trying to loop through a CSV file which
I can do. What I want to change though is for the loop to start at
row 2 in the file thus excluding column headers.
At present I am using this statement to initiate a loop though the
records:
for line in f.readlines():
How do I start this at row 2?

The quick answer to your literal question is:
for line in f.readlines()[1:]:
or, with extreme loss of elegance, this:
for lino, line in enumerate(f.readlines()):
if not lino:
continue

But readline and readlines are old hat, and you wouldn't want to read
a file of a few million lines into a big list, so a better answer is:

_unused = f.next()
for line in f:

But you did say you were reading a CSV file, and you don't really want
to do your own CSV parsing, even if you think you know how to get it
right, so best is:

import csv
rdr = csv.reader(f)
heading_row = rdr.next()
for data_row in rdr:

HTH,
John

Thanks so much for the quick response. All working now.

I had looked at the CSV module but when I ran into another problem of
trying to loop through all columns, I was given a solution in another
forum which used the readlines() method.

I have looked at the CSV documentation but didn't see any mention of
heading_row or data_row. Is there a definitive Python documentation
site with code examples like MS's MSDN?
 
J

John Machin

The quick answer to your literal question is:
for line in f.readlines()[1:]:
or, with extreme loss of elegance, this:
for lino, line in enumerate(f.readlines()):
if not lino:
continue
But readline and readlines are old hat, and you wouldn't want to read
a file of a few million lines into a big list, so a better answer is:
_unused = f.next()
for line in f:
But you did say you were reading a CSV file, and you don't really want
to do your own CSV parsing, even if you think you know how to get it
right, so best is:
import csv
rdr = csv.reader(f)
heading_row = rdr.next()
for data_row in rdr:
HTH,
John

Thanks so much for the quick response. All working now.

I had looked at the CSV module but when I ran into another problem of
trying to loop through all columns, I was given a solution in another
forum which used the readlines() method.

Antique advice which still left you doing the CSV parsing :)
I have looked at the CSV documentation but didn't see any mention of
heading_row or data_row.

Ummm ... 'heading_row' and 'data_row' are identifiers of the kind that
you or I would need to make up in any language; why did you expect to
find them in the documentation?
Is there a definitive Python documentation
site with code examples like MS's MSDN?

The definitive Python documentation site is (I suppose) http://www.python.org/doc/

I don't know what "code examples like MS's MSDN" means. I avoid MSDN
like I'd avoid a nurse carrying a bottle of Dettol and a wire
brush :)

Here's an example of sucking your data into a list of lists and
accessing it columnwise:

rdr = csv.reader(f)
whatever_you_want_to_call_the_heading_row = rdr.next()
data = list(rdr)
sum_col_5 = sum(float(row[5]) for row in data)
print "The value in row 3, column 4 is", data[3][4]

HTH,
John
 
T

Thomas Nelson

The DictReader object automatically does this for you:
.... Tom,21,programmer
.... Sara,22,chef
.... Molly,23,doctor""".... print line
....
{'job': 'programmer', 'age': '21', 'Name': 'Tom'}
{'job': 'chef', 'age': '22', 'Name': 'Sara'}
{'job': 'doctor', 'age': '23', 'Name': 'Molly'}

HTH,
Tom
 
?

=?ISO-8859-1?Q?Ricardo_Ar=E1oz?=

John said:
I am a Python beginner. I am trying to loop through a CSV file which
I can do. What I want to change though is for the loop to start at
row 2 in the file thus excluding column headers.
At present I am using this statement to initiate a loop though the
records:
for line in f.readlines():
How do I start this at row 2?
The quick answer to your literal question is:
for line in f.readlines()[1:]:
or, with extreme loss of elegance, this:
for lino, line in enumerate(f.readlines()):
if not lino:
continue
But readline and readlines are old hat, and you wouldn't want to read
a file of a few million lines into a big list, so a better answer is:
_unused = f.next()
for line in f:
But you did say you were reading a CSV file, and you don't really want
to do your own CSV parsing, even if you think you know how to get it
right, so best is:
import csv
rdr = csv.reader(f)
heading_row = rdr.next()
for data_row in rdr:
HTH,
John
Thanks so much for the quick response. All working now.

I had looked at the CSV module but when I ran into another problem of
trying to loop through all columns, I was given a solution in another
forum which used the readlines() method.

Antique advice which still left you doing the CSV parsing :)
I have looked at the CSV documentation but didn't see any mention of
heading_row or data_row.

Ummm ... 'heading_row' and 'data_row' are identifiers of the kind that
you or I would need to make up in any language; why did you expect to
find them in the documentation?
Is there a definitive Python documentation
site with code examples like MS's MSDN?

The definitive Python documentation site is (I suppose) http://www.python.org/doc/

I don't know what "code examples like MS's MSDN" means. I avoid MSDN
like I'd avoid a nurse carrying a bottle of Dettol and a wire
brush :)

Here's an example of sucking your data into a list of lists and
accessing it columnwise:

rdr = csv.reader(f)
whatever_you_want_to_call_the_heading_row = rdr.next()
data = list(rdr)
sum_col_5 = sum(float(row[5]) for row in data)
print "The value in row 3, column 4 is", data[3][4]

HTH,
John

Given that you have a header row you could also do (untested):

rdr = csv.DictReader(f)
data = list(rdr)
sum_Value = sum(float(row['Value']) for row in data)
print "The value in row 3, column 'ColName' is", data[3]['ColName']

DictReader generates a list of dictionaries which take the keys from
your header row.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top