I'm trying to use the tostring method in XML to get a "pretty" version of my XML as a string. The example on the lxml site shows this example:
>>> 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? 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. To print that as a unicode string (which is the typical encoding for a Python string), you can do:
print(etree.tostring(root,pretty_print=True).decode())
or etree.tostring
has a flag that allows you to set the encoding, so:
print(etree.tostring(root,pretty_print=True,encoding='unicode'))
Either way works for me. Here's more information on Byte Strings and Strings
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With