Shaner
Shaner

Reputation: 63

"pretty_print" does not work well for xml shown in python3.x

There is a string object with xml content. In python, I used etree to pretty print it. It works well in python 2.x, but fails to be shown in python 3.x.

from lxml import etree
print etree.tostring(etree.fromstring(patternXML), pretty_print=True)

I expect the xml is shown well as line breaks, but the actual output is bytes type and "\n" is shown instead of line break.

Upvotes: 1

Views: 130

Answers (1)

Shaner
Shaner

Reputation: 63

It has been resolved by changing "tostring" to "tounicode" as below.

print etree.tounicode(etree.fromstring(patternXML), pretty_print=True)

Upvotes: 4

Related Questions