【问题标题】:Parsing XML with ElementTree "xml.parsers.expat.ExpatError: not well-formed (invalid token)"使用 ElementTree “xml.parsers.expat.ExpatError: not well-formed (invalid token)”解析 XML
【发布时间】:2019-02-14 14:29:17
【问题描述】:

我遵循这个线程的帮助。 How do I get Python's ElementTree to pretty print to an XML file?

但是我有一个我不明白的错误。

错误

File "myprogram.py", line 505, in get_phyloxml_extended
xmlstr = minidom.parseString(ET.tostring(root).decode("utf-8")).toprettyxml(indent="   ")
File "/usr/lib/python3.6/xml/dom/minidom.py", line 1968, in parseString
return expatbuilder.parseString(string)
File "/usr/lib/python3.6/xml/dom/expatbuilder.py", line 925, in parseString
return builder.parseString(string)
File "/usr/lib/python3.6/xml/dom/expatbuilder.py", line 223, in parseString
parser.Parse(string, True)
xml.parsers.expat.ExpatError: not well-formed (invalid token): line 1, column 425

代码

tree = ET.ElementTree(ET.fromstring(xml_string))
root = tree.getroot()
xmlstr = minidom.parseString(ET.tostring(root).decode("utf-8")).toprettyxml(indent="   ")
with open(file_ext_phyloxml_path, "w") as f:
    f.write(xmlstr.encode('utf-8'))

【问题讨论】:

    标签: xml python-3.x elementtree


    【解决方案1】:

    其中一个标签是数字,所以它不起作用.....

    点赞<phy:00000001>2.0<phy:00000001> 不行

    但是……

    但是……

    <phy:a00000001>2.0<phy:a00000001>工作--'

    【讨论】:

    猜你喜欢
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-14
    • 1970-01-01
    • 1970-01-01
    • 2015-05-16
    • 1970-01-01
    相关资源
    最近更新 更多