[Tutor] Pretty printing XML using LXML on Python3

SM sunithanc at gmail.com
Fri Nov 29 22:21:22 CET 2013


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.
I will figure out why.
-SM


On Thu, Nov 28, 2013 at 2:45 PM, eryksun <eryksun at gmail.com> wrote:

> 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>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20131129/9a42d51f/attachment-0001.html>


More information about the Tutor mailing list