Cristhian Boujon
Cristhian Boujon

Reputation: 4190

Getting original string with lxml tostring()

I'm using lxml and I'm trying to get the original xml string so:

>>> elem = etree.fromstring("<tag>áéíóúñü</tag>")
>>> etree.tostring(elem)
b'<tag>&#225;&#233;&#237;&#243;&#250;&#241;&#252;</tag>'

The only way that I found to get the original string is doing:

>>> etree.tostring(elem, encoding = "utf-8").decode("utf-8")
'<tag>áéíóúñü</tag>'

Is there a better aproach?

Upvotes: 4

Views: 3755

Answers (1)

Nathan Vērzemnieks
Nathan Vērzemnieks

Reputation: 5603

How about this:

In [31]: elem = etree.fromstring("<tag>❄</tag>")

In [32]: etree.tostring(elem, encoding=str)
Out[32]: '<tag>❄</tag>'

Upvotes: 3

Related Questions