假设我读了一个html网站,我得到了一个名单,例如:'Amiel,Henri-Frédéric'.
为了获取名称列表,我使用以下代码解码html:
f = urllib.urlopen("http://xxx.htm")
html = f.read()
html=html.decode('utf8')
t.feed(html)
t.close()
lista=t.data
Run Code Online (Sandbox Code Playgroud)
此时,变量lista包含一个名称列表,如:
[u'Abatantuono,Diego',...,u'Amiel,Henri-Frédéric']
现在我想:
为简单起见,我们只考虑上面的名称来完成步骤1到3.我将使用以下代码:
name=u'Amiel, Henri-Fr\xe9d\xe9ric'
name=name.encode('utf8')
array=[name]
df=pd.DataFrame({'Names':array})
df.to_csv('names')
uni=pd.read_csv('names')
uni #trying to read the csv file in a DataFrame
Run Code Online (Sandbox Code Playgroud)
此时我收到以下错误:
UnicodeDecodeError: 'utf8' codec can't decode byte 0xe9 in position 67: invalid continuation byte
Run Code Online (Sandbox Code Playgroud)
如果我用以下代码替换上面代码的最后一行:
print uni
Run Code Online (Sandbox Code Playgroud)
我可以阅读DataFrame,但我不认为这是处理这个问题的正确方法.
我向其他用户发布了很多关于这个论点的问题,但我没有解决这个问题.