首页 > 解决方案 > 如何使用带有 unicode 的 python 2.7 ElementTree 模块呈现 xml

问题描述

我有一些代码在 unicode 输入上引发致命异常。我正在使用 ElementTree 构建一个 xml 文档并使用 tostring() 来打印它。我试过传入 unicode 对象,并将它们编码为 UTF-8 字节串,这没有区别。我不知道我做错了什么或者模块中是否有错误。

这是一个重现它的小样本。

#!/usr/bin/python

from __future__ import unicode_literals, print_function
from xml.etree.ElementTree import Element, SubElement, tostring
import xml.etree.ElementTree as ET
import time

def main():
    xml = Element('build_summary')
    mpversion = SubElement(xml, 'magpy_version')
    mpversion.text = '1.2.3.4'
    version = SubElement(xml, 'version')
    version.text = '11.22.33.44'
    date = SubElement(xml, 'date')
    date.text = time.strftime("%a %b %-d %Y", time.localtime())
    args = SubElement(xml, 'args')
    args.text = 'build args'
    issues = SubElement(xml, 'issues')
    # Add the repos and the changes in them
    changelog = 'this is the changelog \u2615'
    #changelog = 'this is the changelog'
    print("adding changelog:", changelog)
    repository = SubElement(issues, 'repo')
    reponame = SubElement(repository, 'reponame')
    reponame.text = 'repo name'
    repoissues = SubElement(repository, 'repoissues')
    #repoissues.text = changelog.encode('UTF-8', 'replace')
    repoissues.text = changelog

    # Generate a string, reparse it, and pretty-print it.
    #ET.dump(xml)
    #xml.write('myoutput.xml')
    rough = tostring(xml, encoding='UTF-8', method='xml')
    #rough = tostring(xml)
    print(rough)

if __name__ == '__main__':
    main()

这会产生以下结果:

msoulier@anton:~$ python treetest.py
adding changelog: this is the changelog ☕
Traceback (most recent call last):
File "treetest.py", line 38, in <module>
    main()
File "treetest.py", line 33, in main
    rough = tostring(xml, encoding='UTF-8', method='xml')
File "/usr/local/Cellar/python@2/2.7.16/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 1127, in tostring
    return "".join(data)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 22: ordinal not in range(128)

那么,我在这里做错了什么?奇怪的是,ElementTree.dump 工作正常,但文档说不要将它用于任何错误调试。

标签: pythonxmlpython-2.7unicodeelementtree

解决方案


推荐阅读