我不断得到:
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")
最佳答案
它对我有用的方式:
html = df.to_html()
with open("dataframe.html", "w", encoding="utf-8") as file:
file.writelines('<meta charset="UTF-8">\n')
file.write(html)
关于python - 如何使用 utf-8 编码将 DataFrame 导出到 Html?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35894576/