解决Python中pandas读取*.csv文件出现编码问题
Error in sitecustomize; set PYTHONVERBOSE for traceback:
NameError: name ‘reload’ is not defined
Traceback (most recent call last):
File “C:/Users/wen/Desktop/pisx_git/B2020931-PX-GWC-Services/rdsystem/jiaoben/bk/jiaoben02.py”, line 75, in
articleDF = pd.read_csv(“11.csv”,encoding=encoding)
File “C:\Users\wen\AppData\Roaming\Python\Python37\site-packages\pandas\io\parsers.py”, line 676, in parser_f
return _read(filepath_or_buffer, kwds)
File “C:\Users\wen\AppData\Roaming\Python\Python37\site-packages\pandas\io\parsers.py”, line 448, in _read
parser = TextFileReader(fp_or_buf, **kwds)
File “C:\Users\wen\AppData\Roaming\Python\Python37\site-packages\pandas\io\parsers.py”, line 880, in init
self._make_engine(self.engine)
File “C:\Users\wen\AppData\Roaming\Python\Python37\site-packages\pandas\io\parsers.py”, line 1114, in _make_engine
self._engine = CParserWrapper(self.f, **self.options)
File “C:\Users\wen\AppData\Roaming\Python\Python37\site-packages\pandas\io\parsers.py”, line 1891, in init
self._reader = parsers.TextReader(src, **kwds)
File “pandas_libs\parsers.pyx”, line 529, in pandas._libs.parsers.TextReader.cinit
File “pandas_libs\parsers.pyx”, line 720, in pandas._libs.parsers.TextReader._get_header
File “pandas_libs\parsers.pyx”, line 916, in pandas._libs.parsers.TextReader._tokenize_rows
File “pandas_libs\parsers.pyx”, line 2063, in pandas._libs.parsers.raise_parser_error
UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0xb1 in position 36: invalid start byte
在window环境中使用pandas读取csv文件时,一直反复出现这个错误,尝试过各种办法,一直出问题。
包括用# encoding = “ANSI”
encoding=“utf-8”
encoding=‘gb2312’
encoding=‘gbk’
都不行。
后来想起做量化交易,终于发现了问题所在。
datas = pd.read_csv("11.csv",engine='python')
通过指定engine为python,立刻解决~