skyhouse 发表于 2022-10-24 17:05:33

求助,读取csv文件出现错误,帮忙瞧瞧

代码
import pandas as pd
import numpy as np
date = pd.read_csv("g:\测试.csv",)
data



错误提示
Traceback (most recent call last):
File "C:/Users/Administrator/PycharmProjects/pythonProject/venv/Scripts/2.py", line 3, in <module>
    date = pd.read_csv("g:\测试.csv")
File "C:\Users\Administrator\PycharmProjects\pythonProject\venv\lib\site-packages\pandas\util\_decorators.py", line 211, in wrapper
    return func(*args, **kwargs)
File "C:\Users\Administrator\PycharmProjects\pythonProject\venv\lib\site-packages\pandas\util\_decorators.py", line 331, in wrapper
    return func(*args, **kwargs)
File "C:\Users\Administrator\PycharmProjects\pythonProject\venv\lib\site-packages\pandas\io\parsers\readers.py", line 950, in read_csv
    return _read(filepath_or_buffer, kwds)
File "C:\Users\Administrator\PycharmProjects\pythonProject\venv\lib\site-packages\pandas\io\parsers\readers.py", line 605, in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)
File "C:\Users\Administrator\PycharmProjects\pythonProject\venv\lib\site-packages\pandas\io\parsers\readers.py", line 1442, in __init__
    self._engine = self._make_engine(f, self.engine)
File "C:\Users\Administrator\PycharmProjects\pythonProject\venv\lib\site-packages\pandas\io\parsers\readers.py", line 1753, in _make_engine
    return mapping(f, **self.options)
File "C:\Users\Administrator\PycharmProjects\pythonProject\venv\lib\site-packages\pandas\io\parsers\c_parser_wrapper.py", line 79, in __init__
    self._reader = parsers.TextReader(src, **kwds)
File "pandas\_libs\parsers.pyx", line 547, in pandas._libs.parsers.TextReader.__cinit__
File "pandas\_libs\parsers.pyx", line 636, in pandas._libs.parsers.TextReader._get_header
File "pandas\_libs\parsers.pyx", line 852, in pandas._libs.parsers.TextReader._tokenize_rows
File "pandas\_libs\parsers.pyx", line 1965, in pandas._libs.parsers.raise_parser_error
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xcd in position 0: invalid continuation byte

suchocolate 发表于 2022-10-24 19:37:57

测试.csv分享一下,如果没权限传附件,就用度盘或其他什么网盘

卡卡卡1 发表于 2022-10-24 20:25:17

要把CSV文件打开,保存为utf-8类型的文件

skyhouse 发表于 2022-10-25 15:39:33

谢谢以上两位热心朋友,后面找到方法了,用Notepad++打开csv文件,看下右下角的格式,发现是gd2312.回到程序里加encoding='gd2312'
页: [1]
查看完整版本: 求助,读取csv文件出现错误,帮忙瞧瞧