python - 如何使用带有 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 工作正常,但文档说不要将它用于任何错误调试。
解决方案
推荐阅读
- javascript - R的qnorm函数有javascript实现吗?
- ios - 可访问性焦点位于表格视图中单元格之间的空间中
- node.js - NestJS 无法解析服务依赖
- python - 在 keras 中制作音频到音频模型
- python - 高效的矩阵-矩阵乘积
- laravel - 如何在 laravel 中获取具有特定角色的用户及其个人资料详细信息
- python - 在 python 中有没有办法向没有安装 python 的人显示 CLI 应用程序
- django - 具有两个字段的 django 字段。需要意见
- python - 特定行列的平均值
- internet-explorer - 我的 IE 浏览器版本是 11.239.* 但是当我检查代码时它显示版本为 IE 版本 7