Skip to content Skip to sidebar Skip to footer

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. The example on the lxml site shows this example: >>> import lxml.etree as etr

Solution 1:

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

Post a Comment for "Pretty_print Option In Tostring Not Working In Lxml"