tostring中的pretty_print选项在lxm中不起作用

2024-10-01 00:31:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用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版本中是否存在错误?教程中的逐字例句不起作用似乎很奇怪。


Tags: 方法字符串版本示例rootxmlelementlxml