read_csv()报错: 'utf-8' codec can't decode byte 0xca in position 0: invalid continuation byte最新解决办法

想通过pandas中的read_csv()读取csv文件,结果一运行就报错

>>>import pandas as pd
>>>df = pd.read_csv('C:/Users/Administrator/Desktop/1.csv')
Traceback (most recent call last):
  File "pandas\_libs\parsers.pyx", line 1169, in pandas._libs.parsers.TextReader._convert_tokens
  File "pandas\_libs\parsers.pyx", line 1299, in pandas._libs.parsers.TextReader._convert_with_dtype
  File "pandas\_libs\parsers.pyx", line 1315, in pandas._libs.parsers.TextReader._string_convert
  File "pandas\_libs\parsers.pyx", line 1553, in pandas._libs.parsers._string_box_utf8
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xca in position 0: invalid continuation byte
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "D:\Python\Python37-32\lib\site-packages\IPython\core\interactiveshell.py", line 3325, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "", line 1, in 
    df = pd.read_csv('C:/Users/Administrator/Desktop/1.csv')
  File "D:\Python\Python37-32\lib\site-packages\pandas\io\parsers.py", line 702, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "D:\Python\Python37-32\lib\site-packages\pandas\io\parsers.py", line 435, in _read
    data = parser.read(nrows)
  File "D:\Python\Python37-32\lib\site-packages\pandas\io\parsers.py", line 1139, in read
    ret = self._engine.read(nrows)
  File "D:\Python\Python37-32\lib\site-packages\pandas\io\parsers.py", line 1995, in read
    data = self._reader.read(nrows)
  File "pandas\_libs\parsers.pyx", line 899, in pandas._libs.parsers.TextReader.read
  File "pandas\_libs\parsers.pyx", line 914, in pandas._libs.parsers.TextReader._read_low_memory
  File "pandas\_libs\parsers.pyx", line 991, in pandas._libs.parsers.TextReader._read_rows
  File "pandas\_libs\parsers.pyx", line 1123, in pandas._libs.parsers.TextReader._convert_column_data
  File "pandas\_libs\parsers.pyx", line 1176, in pandas._libs.parsers.TextReader._convert_tokens
  File "pandas\_libs\parsers.pyx", line 1299, in pandas._libs.parsers.TextReader._convert_with_dtype
  File "pandas\_libs\parsers.pyx", line 1315, in pandas._libs.parsers.TextReader._string_convert
  File "pandas\_libs\parsers.pyx", line 1553, in pandas._libs.parsers._string_box_utf8
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xca in position 0: invalid continuation byte
搜索到的答案都是说需要将utf-8格式修改为gbk编码,或者在文档保存时修改编码为gbk,尝试发现用wps保存时没有编码格式选项,用office软件保存时只有两种csv保存文件格式

read_csv()报错: 'utf-8' codec can't decode byte 0xca in position 0: invalid continuation byte最新解决办法_第1张图片

最后发现选择用CSV UTF-8(逗号分隔)(*.csv)这种文件格式保存后文件可正常被读取。

如果有 visual studio code 软件的话,直接用该软件打开文档,修改coding为 UTF-8也是可行的

在这里插入图片描述

你可能感兴趣的:(python,0xca,read_csv(),utf-8,invalid,continuation,byte)