Getting source string using lxml tostring ()

I am using lxml and I am trying to get the original xml string like this:

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

The only way I found to get the source string:

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

Is there a better aproach?

+4
source share
1 answer

How about this:

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

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

Source: https://habr.com/ru/post/1676312/


All Articles