【发布时间】: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