954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to use python read from a certain row in CSV file

Hi everyone,

I need to use python to read some data in my csv file, but the file I have contains 15 rows of introduction in front. all the data I need is from the row 16. I am able to read the data as the way I want it if I remove the first 15 rows, but it's very troublesome to do it every time and I have a lot of csv files like that.

so I want to know if there is a way to let python skip the first 15 rows just read from row 16?

below is my code

import csv
import time
import datetime

tempReadings = csv.reader(open('newdata3.csv','rb'),delimiter=',')

for reading in tempReadings:

print "got: %s" % reading
(timestamp, format, temp) = reading

print "timestamp: %s" % timestamp

print "temp: %s" % temp

Thanks

StefanieZY
Newbie Poster
1 post since Nov 2011
Reputation Points: 10
Solved Threads: 0
 
import csv
import time
import datetime

tempReadings = csv.reader(open('newdata3.csv','rb'),delimiter=',')

for reading in tempReadings:

    print "got: %s" % reading
    (timestamp, format, temp) = reading

    print "timestamp: %s" % timestamp

    print "temp: %s" % temp

Learn to push[CODE] before paste.
I think you can use itertools.islice to read from 16th value in the csv.reader. I mostly use manual split, strip routine instead of csv. If I had quoted values in files, then csv module would be good choice, otherwise I DIY. If you have choice, for me the best separator choice is semi comma.

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: