[Tutor] Pretty printing XML using LXML on Python3

eryksun eryksun at gmail.com
Thu Nov 28 20:45:59 CET 2013


On Thu, Nov 28, 2013 at 2:12 PM, SM <sunithanc at gmail.com> 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__. 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>


More information about the Tutor mailing list