如何使用utf-8编码将DataFrame导出到HTML? [英] How to export DataFrame to Html with utf-8 encoding?

查看:203
本文介绍了如何使用utf-8编码将DataFrame导出到HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不断得到:

UnicodeEncodeError: 'ascii' codec can't encode characters in position 265-266: ordinal not in range(128)

当我尝试:

df.to_html("mypage.html")

下面是如何重现该问题的示例:

here is a sample of how to reproduce the problem:

df = pd.DataFrame({"a": [u'Rue du Gu\xc3\xa9, 78120 Sonchamp'], "b": [u"some other thing"]})
df.to_html("mypage.html")

"a"中的元素列表为"unicode"类型.

the list of elements in "a" are of type "unicode".

当我想将其导出到csv时,它可以工作,因为您可以这样做:

when I want to export it to csv it works because you can do:

df.to_csv("myfile.csv", encoding="utf-8")

推荐答案

您的问题在其他代码中.您的示例代码包含一个Unicode字符串,该字符串被误解码为latin1Windows-1252或类似名称,因为其中包含UTF-8序列.在这里,我撤消了错误的解码,并将其重新编码为UTF-8,但是您将要查找在哪里执行了错误的解码:

Your problem is in other code. Your sample code has a Unicode string that has been mis-decoded as latin1, Windows-1252, or similar, since it has UTF-8 sequences in it. Here I undo the bad decoding and redecode as UTF-8, but you'll want to find where the wrong decode is being performed:

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

这篇关于如何使用utf-8编码将DataFrame导出到HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆