简体   繁体   English

elementTree无法解析Unicode XML

[英]elementTree cannot parse unicode xml

I have following xml: 我有以下xml:

<Earth>
 <country name="Česká republika" population="8900000">
    <capital>Praha1</capital>        
  </country>
</Earth>

But when I try to parse it fails with error: 但是当我尝试解析时,它失败并显示错误:

 xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 2, column 20

my code: 我的代码:

 tree=etree.parse(input) # input -> file.xml

As arhimmel pointed out, the issue is likely an encoding issue. 正如arhimmel指出的那样,该问题很可能是编码问题。 etree.parse allows passing file-like objects as well as paths, so you could try adding import codecs at the top of your code and then replacing input with codecs.open("file.xml", encoding="UTF-8") . etree.parse允许传递类似文件的对象以及路径,因此您可以尝试在代码顶部添加import codecs ,然后用codecs.open("file.xml", encoding="UTF-8")替换input

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM