Home > Enterprise >  pd.read_csv with error: Expected 2 fields in line 52, saw 3
pd.read_csv with error: Expected 2 fields in line 52, saw 3

Time:10-25

I'm needing to read some CSV files, and am getting errors regarding columns of the file:

extracted_file = pd.read_csv(filename)
ParserError: Error tokenizing data. C error: Expected 2 fields in line 52, saw 3

Upon investigation of my files, I feel like it is the way they are formatted starting from line 52 where the error is, with an extra comma at the end of each line:

(line 50) Name, xxx
(line 51) Serial, yyy
(line 52) 1, 5.00,
(line 53) 2,6.00,
(line 54) 3,7.00,

When I try to fix this using:

extracted_file = pd.read_csv(filename, error_bad_lines=False)

extracted_file only shows up to line 44 in my data. Inconveniently, I really only need the data from line 52 onwards! Any way to successfully import this?

Thanks!

CodePudding user response:

You can try to set the parameter skiprows=51.
pd.read_csv(filename, skiprows=51)

  • Related