2

I have somes csvs where the fields names are on the third line. The two first line are comment and the true csv start at the third line.

What is the best way to use csv.DictReader to start at the third line not the first line ?

Regards

Bussiere
  • 500
  • 13
  • 60
  • 119
  • Depending on what you're doing with the data, you could also use `pandas.read_csv()` with the `skiprows` parameter. – AMC Jan 23 '20 at 23:16
  • 3
    Does this answer your question? [Skip first couple of lines while reading lines in Python file](https://stackoverflow.com/questions/9578580/skip-first-couple-of-lines-while-reading-lines-in-python-file) – AMC Jan 23 '20 at 23:17

1 Answers1

4

you can call next(f) twice (where f is the file handler) and then pass it to csv.DcitReader() constructor, e.g:

import csv
with open('so.csv') as f:
    next(f)
    next(f)
    dict_rdr = csv.DictReader(f)
    for line in dict_rdr:
        print(line)
buran
  • 13,682
  • 10
  • 36
  • 61