![](/img/trans.png)
[英]python - Parse XML with unicode characters into ElementTree
[英]elementTree cannot parse unicode xml
我有以下xml:
<Earth>
<country name="Česká republika" population="8900000">
<capital>Praha1</capital>
</country>
</Earth>
但是当我尝试解析时,它失败并显示错误:
xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 2, column 20
我的代码:
tree=etree.parse(input) # input -> file.xml
正如arhimmel指出的那样,该问题很可能是编码问题。 etree.parse允许传递类似文件的对象以及路径,因此您可以尝试在代码顶部添加import codecs
,然后用codecs.open("file.xml", encoding="UTF-8")
替换input
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.