将原文件以记事本打开,另存为utf-8格式。
(文件量少的时候直接用这个方法百试不爽)
用rb+打开,然后解码
with open(f'hahaha.html','rb+') as f:
html=f.read().decode('utf-8','ignore')
替换/忽略错误
str = unicode(str, errors='replace')
#或者
str = unicode(str, errors='ignore')
使用codecs包
import codecs
with codecs.open(file_name, 'r', encoding='utf-8',
errors='ignore') as fdata:
使用python引擎(默认引擎为C)
pd.read_csv(gdp_path, sep='\t', engine='python')
解码再编码
str = str.decode('unicode_escape').encode('utf-8')
使用python3
python2默认编码不是utf-8
参考文献
1.UnicodeDecodeError: ‘utf8’ codec can’t decode byte 0x9c https://stackoverflow.com/questions/12468179/unicodedecodeerror-utf8-codec-cant-decode-byte-0x9c