0

In line with this question, Let's say we have the following .csv file to read with Python 3.7:

sep=,
A,B,C
1,2,3
4,"5,3",6

The Python code looks like this:

import csv

with open("tstSO.csv") as csv_file:
    csv_reader = csv.DictReader(csv_file)
    for row in csv_reader:
        thisA = row["A"]
        print(thisA)

Running this, I get KeyError: 'A' in the error message. If I remove the sep=, line, then I get the expected output:

1 
4

Is there a way I can make python ignore/skip the line that indicated the delimiter? The actual files I need to work with are very large, so it would not be nice to do copy operations or manipulations meddling with the whole file. Of course, I also need to make operations on other columns and values, the code above is just a minimal working example.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
Mefitico
  • 816
  • 1
  • 12
  • 41
  • 1
    `next(csv_file)` right before `csv_reader = csv.DictReader(csv_file)` skips the first line – Patrick Artner Mar 12 '19 at 17:35
  • 1
    Just want to emphasize that the use of `csv.Sniffer()` in the answer to the linked duplicate question isn't needed here. Just skip the line as @PatrickArtner suggests. – martineau Mar 12 '19 at 18:22

0 Answers0