用Python3读取CSV类型文件时出现无效字节延续的问题

用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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值