Pretty_print parameter in tostring not working in lxml

I am trying to use the tostring method in XML to get a โ€œprettyโ€ version of my XML as a string. An 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 executing these exact lines is:

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

Is there an error in the lxml version that I installed? It seems strange that the phrase from the textbook does not work.

+8
python xml lxml
source share
1 answer

the b flag in front of the line shows you that it is a byte line. To print this as a Unicode string (which is a 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, therefore:

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

In any case, it works for me. Here is more information about byte strings and Strings

+13
source share

All Articles