UnicodeDecodeError:“utf8”编解码器无法解码位置5中的字节0xa0:无效的开始字节

2024-07-08 07:14:50 发布

您现在位置:Python中文网/ 问答频道 /正文

代码:

import pandas as pd

data = pd.read_csv('db.csv')
data.head()
data.drop(['Company Rate', 'Metascore', 'Minutes Release Budget', 'Opening Weekend USA', 'Gross USA'], axis=0)

data.to_csv('db2.csv', encoding='utf-8')

错误消息:

Traceback (most recent call last):
  File "/Users/christine/Documents/Christine-CS/ALT 2/ALT2 Project/clean db2.py", line 3, in <module>
    data = pd.read_csv('db.csv')
  File "/Users/christine/Library/Python/3.7/lib/python/site-packages/pandas/io/parsers.py", line 688, in read_csv
    return _read(filepath_or_buffer, kwds)
  File "/Users/christine/Library/Python/3.7/lib/python/site-packages/pandas/io/parsers.py", line 454, in _read
    parser = TextFileReader(fp_or_buf, **kwds)
  File "/Users/christine/Library/Python/3.7/lib/python/site-packages/pandas/io/parsers.py", line 948, in __init__
    self._make_engine(self.engine)
  File "/Users/christine/Library/Python/3.7/lib/python/site-packages/pandas/io/parsers.py", line 1180, in _make_engine
    self._engine = CParserWrapper(self.f, **self.options)
  File "/Users/christine/Library/Python/3.7/lib/python/site-packages/pandas/io/parsers.py", line 2010, in __init__
    self._reader = parsers.TextReader(src, **kwds)
  File "pandas/_libs/parsers.pyx", line 537, in pandas._libs.parsers.TextReader.__cinit__
  File "pandas/_libs/parsers.pyx", line 740, in pandas._libs.parsers.TextReader._get_header

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa0 in position 5: invalid start byte

Tags: csvinpyselfpandasreaddatalib

热门问题