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.
python xml lxml
lanteau
source share