简体   繁体   English

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

[英]pretty_print option in tostring not working in lxml

I'm trying to use the tostring method in XML to get a "pretty" version of my XML as a string. 我正在尝试在XML中使用tostring方法来获取XML的“漂亮”版本作为字符串。 The example on the lxml site shows this example: 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>

However my output, running those exact lines is: 但是我的输出,运行那些确切的行是:

b'<root>\n  <child1/>\n  <child2/>\n  <child3/>\n</root>\n'

Is there a bug in the version of lxml I have installed? 我安装的lxml版本是否有错误? It seems odd the word for word example from the tutorial is not working. 从教程中逐字逐句的单词似乎很奇怪。

the b flag in front of the string shows you that it's a byte string . 字符串前面的b标志显示它是一个字节字符串 To print that as a unicode string (which is the typical encoding for a Python string), you can do: 要将其打印为unicode字符串(这是Python字符串的典型编码),您可以执行以下操作:

print(etree.tostring(root,pretty_print=True).decode())

or etree.tostring has a flag that allows you to set the encoding, so: 或者etree.tostring有一个标志,允许您设置编码,因此:

print(etree.tostring(root,pretty_print=True,encoding='unicode'))

Either way works for me. 无论哪种方式都适合我。 Here's more information on Byte Strings and Strings 这里有关于Byte StringsStrings的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM