[Tutor] Pretty printing XML using LXML on Python3

Stefan Behnel stefan_ml at behnel.de
Sat Nov 30 10:04:38 CET 2013


SM, 29.11.2013 22:21:
> On Thu, Nov 28, 2013 at 2:45 PM, eryksun wrote:
>> On Thu, Nov 28, 2013 at 2:12 PM, SM wrote:
>>> Run with Python3:
>>>
>>> $ python3 testx.py
>>> b'<root>\n  <child/>\n  <child>some text</child>\n</root>\n'
>>
>> print() first gets the object as a string. tostring() returns bytes,
>> and bytes.__str__ returns the same as bytes.__repr__.

Meaning, it's a pure matter of visual representation on the screen, not a
difference in the data.


>> You can decode
>> the bytes before printing, or instead use tounicode():
>>
>>     >>> s = etree.tounicode(root, pretty_print=True)
>>     >>> print(s)
>>     <root>
>>       <child/>
>>       <child>some text</child>
>>     </root>
> 
> Thank you, eryksun. using tounicode seems to work on this small piece of
> code. It still has issues with my code which is generating a big XML code.

Well, I'm sure you are not generating a large chunk of XML just to print it
on the screen, so using tostring(), as you did before, is certainly better.

However, if it's really that much output, you should serialise into a file
instead of serialising it into memory first and then writing that into a
file. So, use ElementTree.write() to write the output into a file directly.

Stefan




More information about the Tutor mailing list