简体   繁体   中英

Set lxml etree text to string with tags

I have a problem regarding the lxml.etree library. I have a string like

string = "this<a/>is<b/>nice"

and I want to set this string as the text of an element node.

node.text = string

But everytime if I print out the text of the node, its escaped like this:

"this&lt;a\&gt;is&lt;b\&gt;nice"

So how do I set the text so that its not escaped anymore? I cant do it eg with node.tail or anything, because I have more than one node inside the text.

What you could do is add a root element to the string to make it well-formed and then parse it using tostring() . Then you can add the Element as a child of the target element.

Once it's where it's supposed to be, you can use strip_tags() to remove the temporary root element.

Example...

Python

from lxml import etree

doc = etree.fromstring("<doc/>")

print(f"doc before: \"{etree.tostring(doc).decode()}\"")

string = "this<a/>is<b/>nice"

fragment = etree.fromstring(f"<temp>{string}</temp>")
doc.append(fragment)
etree.strip_tags(doc, "temp")

print(f"doc after: \"{etree.tostring(doc).decode()}\"")

Console Output

doc before: "<doc/>"
doc after: "<doc>this<a/>is<b/>nice</doc>"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM