用python代码读取CSV类型文件的数据时出现以下问题解决的办法:
Traceback (most recent call last):
File "C:/Users/gwp/PycharmProjects/LSTM/LSTM.py", line 9, in <module>
series=read_csv('shampoo-sales.csv',encoding='utf-8',header=0,parse_dates=[0],index_col=0,squeeze=True)
File "C:\Users\gwp\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 562, in parser_f
return _read(filepath_or_buffer, kwds)
File "C:\Users\gwp\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 325, in _read
return parser.read()
File "C:\Users\gwp\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 815, in read
ret = self._engine.read(nrows)
File "C:\Users\gwp\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 1314, in read
data = self._reader.read(nrows)
File "pandas\parser.pyx", line 805, in pandas.parser.TextReader.read (pandas\parser.c:8748)
File "pandas\parser.pyx", line 827, in pandas.parser.TextReader._read_low_memory (pandas\parser.c:9003)
File "pandas\parser.pyx", line 904, in pandas.parser.TextReader._read_rows (pandas\parser.c:10022)
File "pandas\parser.pyx", line 1011, in pandas.parser.TextReader._convert_column_data (pandas\parser.c:11397)
File "pandas\parser.pyx", line 1066, in pandas.parser.TextReader._convert_tokens (pandas\parser.c:12230)
File "pandas\parser.pyx", line 1170, in pandas.parser.TextReader._string_convert (pandas\parser.c:13918)
File "pandas\parser.pyx", line 1402, in pandas.parser._string_box_utf8 (pandas\parser.c:19298)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd4 in position 1: invalid continuation byte
出现该问题的原因是:在window系统是用GBK编码,所以只需要将读取文件中的编码类型改为‘gbk’即可:
详细代码:series=read_csv('shampoo-sales.csv',encoding='gbk',header=0,parse_dates=[0],index_col=0,squeeze=True)