I'm trying to read a CSV file in Python and i get some errors.I think there's something wrong with this particular CSV file, because it workes with others that I've tried. This is the code
import pandas as pd
import numpy as np
def execute():
tabel = pd.read_csv("FoodV.csv", index_col=0)
print(tabel, type(tabel))
if __name__ == "__main__":
execute()
and these are the errors
File "pandas/_libs/parsers.pyx", line 801, in pandas._libs.parsers.TextReader.read_low_memory
File "pandas/_libs/parsers.pyx", line 857, in pandas._libs.parsers.TextReader._read_rows
File "pandas/_libs/parsers.pyx", line 843, in pandas._libs.parsers.TextReader._tokenize_rows
File "pandas/_libs/parsers.pyx", line 1925, in pandas._libs.parsers.raise_parser_error
pandas.errors.ParserError: Error tokenizing data. C error: Expected 11 fields in line 3, saw 14 File "pandas/_libs/parsers.pyx", line 801, in pandas._libs.parsers.TextReader.read_low_memory
File "pandas/_libs/parsers.pyx", line 857, in pandas._libs.parsers.TextReader._read_rows
File "pandas/_libs/parsers.pyx", line 843, in pandas._libs.parsers.TextReader._tokenize_rows
File "pandas/_libs/parsers.pyx", line 1925, in pandas._libs.parsers.raise_parser_error
pandas.errors.ParserError: Error tokenizing data. C error: Expected 11 fields in line 3, saw 14
Can you please help?
CodePudding user response:
The problem is explained in the error message :
Expected 11 fields in line 3, saw 14
you probably have a few comma too much on line 3 (or a few missing in previous lines.) it seems that for the CSV to work, you need the same amount of columns for every row, so it can transform it into a pandas dataframe
CodePudding user response:
Your CSV file is corrupted, likely because some inputs have extra (unescaped) commas!
If you can skip them, just set on_bad_lines="warn" when calling .read_csv()
df = pd.read_csv("FoodV.csv", index_col=0, on_bad_lines="warn")
If you need the corrupt lines, you could fix them manually or load the file normally and modify the extra lines
contents = []
with open("FoodV.csv") as fh:
for line in fh: # file-likes are iterable by-lines
fields = lines.split(",") # use a CSV
if len(fields) != 11: # guessed from Question
# fix the line
contents.append(fields)
# create dataframe
