【问题标题】:Unicode not being written to html file pythonUnicode没有被写入html文件python
【发布时间】:2015-06-29 18:26:03
【问题描述】:

所以我使用 minidom 解析带有属性的标签的 xml 文件。我的 python obj 的属性包含这个名为“name”的属性的名称,当我调试它时,它包含这个值“RTÉ News”。问题是我创建了一个帮助对象来为我构建一个用于报告的 html 表。即使这样,当我调试它时,也会在我返回的字符串值中保存“RTÉ News”的内存值以构造单元格“RTÉ News”,但是当我使用此代码时;

with codecs.open(Resources.REPORT, "w", "utf-8") as f:
    reportHelper = ReportsHelper()
    print reportHelper.createResultHTMLReportUnicodeHandled()
    f.write(reportHelper.createHTMLReportUnicodeHandled())
    f.close()

结果:

“RTÉ News”在我的 html 报告中。

任何想法,我是一个java头,只需要学习python一周就可以证明一些事情。伙计们干杯。

【问题讨论】:

    标签: python html unicode utf-8 ascii


    【解决方案1】:

    您的代码似乎是正确的 IMO。为什么要使用

    with codecs.open(Resources.REPORT, "w", "utf-8") as f:
    

    而不是

    with open(Resources.REPORT, 'w', encoding='utf-8') as f:
    

    如果您在浏览器中打开 HTML 文件,您是否尝试过手动设置编码?在 Firefox 19 中:查看 -> 字符编码 -> UTF-8

    我想您的文件已正确编码,但您的 HTML 查看器未正确检测到编码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-19
      • 1970-01-01
      • 2012-01-16
      • 2012-08-30
      • 1970-01-01
      • 2011-06-25
      • 1970-01-01
      相关资源
      最近更新 更多