0

I am writing a Python function to open two .csv files and make changes to the data inside. I am using pandas and pd.read_csv('text') to open the files. Everything works well and the function works for one .csv file. However, when I try it on a different smaller .csv file the file cannot even open.

This is part of the error I am getting when I try to open the .csv file.

Traceback (most recent call last):
  File "C:\Users\...\Downloads\test\test.py", line 3, in <module>
    df = pd.read_csv('data2.csv')
  File "C:\Users\...\AppData\Roaming\Python\Python311\site-packages\pandas\util\_decorators.py", line 211, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\...\AppData\Roaming\Python\Python311\site-packages\pandas\util\_decorators.py", line 331, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\...\AppData\Roaming\Python\Python311\site-packages\pandas\io\parsers\readers.py", line 950, in read_csv
    return _read(filepath_or_buffer, kwds)
  File "C:\Users\...\AppData\Roaming\Python\Python311\site-packages\pandas\io\parsers\readers.py", line 611, in _read
    return parser.read(nrows)
  File "C:\Users\...\AppData\Roaming\Python\Python311\site-packages\pandas\io\parsers\readers.py", line 1778, in read
    ) = self._engine.read(  # type: ignore[attr-defined]
  File "C:\Users\...\AppData\Roaming\Python\Python311\site-packages\pandas\io\parsers\c_parser_wrapper.py", line 230, in read
    chunks = self._reader.read_low_memory(nrows)
  File "pandas\_libs\parsers.pyx", line 808, in pandas._libs.parsers.TextReader.read_low_memory
  File "pandas\_libs\parsers.pyx", line 866, in pandas._libs.parsers.TextReader._read_rows
  File "pandas\_libs\parsers.pyx", line 852, in pandas._libs.parsers.TextReader._tokenize_rows
  File "pandas\_libs\parsers.pyx", line 1973, in pandas._libs.parsers.raise_parser_error
pandas.errors.ParserError: Error tokenizing data. C error: Expected 4 fields in line 8836, saw 5

This is the code I am using to access the .csv files.

import pandas as pd

df = pd.read_csv('test.csv')

All the files are in the correct folders and the file paths are all correct. Any help is appreciated, thanks

6
  • 3
    don't post part of the error. Post the full error message including the stack trace Commented Dec 12, 2022 at 1:38
  • The last thing in the traceback is the error message. We need that part too.
    – tdelaney
    Commented Dec 12, 2022 at 1:38
  • Sorry about that I edited it Commented Dec 12, 2022 at 1:42
  • Your CSV file is malformed: Expected 4 fields in line 8836, saw 5.
    – accdias
    Commented Dec 12, 2022 at 1:42
  • 1
    That failing line would be interesting to look at. Perhaps whatever wrote this CSV didn't escape an internal comma correctly.
    – tdelaney
    Commented Dec 12, 2022 at 2:08

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.