【发布时间】:2023-03-17 15:37:02
【问题描述】:
我正在尝试使用 XML 中的 tostring 方法来获取我的 XML 的“漂亮”版本作为字符串。 lxml 站点上的示例显示了此示例:
>>> import lxml.etree as etree
>>> root = etree.Element("root")
>>> print(root.tag)
root
>>> root.append( etree.Element("child1") )
>>> child2 = etree.SubElement(root, "child2")
>>> child3 = etree.SubElement(root, "child3")
>>> print(etree.tostring(root, pretty_print=True))
<root>
<child1/>
<child2/>
<child3/>
</root>
但是我的输出,运行这些确切的行是:
b'<root>\n <child1/>\n <child2/>\n <child3/>\n</root>\n'
我安装的 lxml 版本有问题吗?教程中逐字逐句的例子似乎很奇怪。
【问题讨论】: