简体   繁体   中英

Reading the CSV file using Pandas results in an error

I have a problem reading the csv file using pandas. Here is the code:

import pandas as pd
import numpy as np
df=pd.read_csv('item.csv')

Here is the error:

Traceback (most recent call last):
  File "Script.py", line 3, in <module>
    df=pd.read_csv('item.csv')
  File "C:\Python38\lib\site-packages\pandas\io\parsers.py", line 605, in read_csv
    return _read(filepath_or_buffer, kwds)
  File "C:\Python38\lib\site-packages\pandas\io\parsers.py", line 457, in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)
  File "C:\Python38\lib\site-packages\pandas\io\parsers.py", line 814, in __init__
    self._engine = self._make_engine(self.engine)
  File "C:\Python38\lib\site-packages\pandas\io\parsers.py", line 1045, in _make_engine
    return mapping[engine](self.f, **self.options)  # type: ignore[call-arg]
  File "C:\Python38\lib\site-packages\pandas\io\parsers.py", line 1893, in __init__
    self._reader = parsers.TextReader(self.handles.handle, **kwds)
  File "pandas\_libs\parsers.pyx", line 518, in pandas._libs.parsers.TextReader.__cinit__
  File "pandas\_libs\parsers.pyx", line 620, in pandas._libs.parsers.TextReader._get_header
  File "pandas\_libs\parsers.pyx", line 814, in pandas._libs.parsers.TextReader._tokenize_rows
  File "pandas\_libs\parsers.pyx", line 1943, in pandas._libs.parsers.raise_parser_error
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc1 in position 11843: invalid start byte

If you add encoding, it will works. Additionally, you can see a link below for standard encodings in python when you need.

data = pd.read_csv(filename, encoding= 'unicode_escape')

https://docs.python.org/3/library/codecs.html#standard-encodings

Try:

import pandas as pd
import numpy as np
df=pd.read_csv(r'item.csv')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM