![](/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.