有一次报错如下:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb3 in position 0: invalid start byte
编码问题:f = open(txtPath,'r',encoding='utf-8')
改为:f = open(txtPath,'r',encoding='gbk')即可
print(f.read())
又有一次读取CSV文件时报错:
import pandas as pd
content = pd.read_csv('news.csv',encoding='utf-8')
print(content.head())
报错如下:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xba in position 0: invalid start byte
这里我将编码问题 utf-8 改为 gbk,还是报错。。。。报错如下:
UnicodeDecodeError: 'gbk' codec can't decode byte 0xa2 in position 93: illegal multibyte sequence
最后当我把程序改为:
import pandas as pd
content = pd.read_csv('news.csv',encoding='gb18030')
print(content.head())
最终成功
在出现编码问题的时候,从编码类型角度入手,往往能够解决问题
with open('film_info.csv','r') as f:
df_fs = pd.read_csv('film_info.csv', sep=',', header=0, delimiter=None, names=None, skiprows=None, nrows=None,encoding='gbk', index_col=None, usecols=None)
想上图,我将utf-8编码改为gbk编码方式之后,就不再报错了