Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python csv headers not in first line

Tags:

python

csv

I have a csv file deliminated by ,. The first line contains the report name, the second line contains the report data. The headers are in line 3 and all the other rows are data. How do I use the csv module to read the headers from line 3 instead of 1? Below is what I have now.

import csv
f = open ("myfile.csv", "rb")
reader = csv.reader(f, delimiter=",")
headers = reader.next()
like image 896
Eric Avatar asked Dec 05 '25 13:12

Eric


1 Answers

You can use itertools.islice to discard items from an iterator.

from itertools import islice
reader = islice(csv.reader(f, delimiter=","), 2, None)
like image 121
Janne Karila Avatar answered Dec 07 '25 15:12

Janne Karila



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!