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

lan*_*eau 12 python xml lxml

我正在尝试在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>
Run Code Online (Sandbox Code Playgroud)

但是我的输出,运行那些确切的行是:

b'<root>\n  <child1/>\n  <child2/>\n  <child3/>\n</root>\n'
Run Code Online (Sandbox Code Playgroud)

我安装的lxml版本是否有错误?从教程中逐字逐句的单词似乎很奇怪.

Ada*_*ith 21

b字符串前面的标志显示它是一个字节字符串.要将其打印为unicode字符串(这是Python字符串的典型编码),您可以执行以下操作:

print(etree.tostring(root,pretty_print=True).decode())
Run Code Online (Sandbox Code Playgroud)

或者etree.tostring有一个允许您设置编码的标志,因此:

print(etree.tostring(root,pretty_print=True,encoding='unicode'))
Run Code Online (Sandbox Code Playgroud)

无论哪种方式都适合我.这里有关于Byte StringsStrings的更多信息