6

I have some lxml element:

>> lxml_element.text
  'hello BREAK world'

I need to replace the word BREAK with an HTML break tag—<br />. I've tried to do simple text replacing:

lxml_element.text.replace('BREAK', '<br />')

but it inserts the tag with escaped symbols, like &lt;br/&gt;. How do I solve this problem?

2 Answers 2

6

Here's how you could do it. Setting up a sample lxml from your question:

>>> import lxml
>>> some_data = "<b>hello BREAK world</b>"
>>> root = lxml.etree.fromstring(some_data)
>>> root
<Element b at 0x3f35a50>
>>> root.text
'hello BREAK world'

Next, create a subelement tag <br>:

>>> childbr = lxml.etree.SubElement(root, "br")
>>> childbr
<Element br at 0x3f35b40>
>>> lxml.etree.tostring(root)
'<b>hello BREAK world<br/></b>'

But that's not all you want. You have to take the text before the <br> and place it in .text:

>>> root.text = "hello"
>>> lxml.etree.tostring(root)
'<b>hello<br/></b>'

Then set the .tail of the child to contain the rest of the text:

>>> childbr.tail = "world"
>>> lxml.etree.tostring(root)
'<b>hello<br/>world</b>'
Sign up to request clarification or add additional context in comments.

2 Comments

Well, I got the same problem and this solution works, but it's a pain to use. Is there no simpler solution or out-of-the-box function for that?
one problem here is if tail contains additional tags. They are wrotten with escaped html entities like &lt
2

Well I don't think you want to just change the text node of the element. What I think you want to do is to modify the text node of your Element add a SubElement of name br to your lxml_element and then set the tail attribute of your subelement to the 2nd part of the string you are parsing. I found the tutorial here: http://lxml.de/tutorial.html#the-element-class to be very useful.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.