我一直在:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 265-266: ordinal not in range(128)
当我尝试:
df.to_html("mypage.html")
这是一个如何重现问题的示例:
df = pd.DataFrame({"a": [u'Rue du Gu\xc3\xa9, 78120 Sonchamp'], "b": [u"some other thing"]})
df.to_html("mypage.html")
“a”中的元素列表是“unicode”类型.
当我想将它导出到csv它是有效的,因为你可以这样做:
df.to_csv("myfile.csv", encoding="utf-8")
解决方法:
您的问题出在其他代码中.您的示例代码具有Unicode字符串,该字符串被错误解码为latin1,Windows-1252或类似字符串,因为它中包含UTF-8序列.在这里我解除了错误的解码并重新编码为UTF-8,但是你想要找到执行错误解码的地方:
>>> s = u'Rue du Gu\xc3\xa9, 78120 Sonchamp'
>>> s.encode('latin1').decode('utf8')
u'Rue du Gu\xe9, 78120 Sonchamp'
>>> print(s.encode('latin1').decode('utf8'))
Rue du Gué, 78120 Sonchamp
标签:python,unicode,pandas
来源: https://codeday.me/bug/20190628/1312408.html