Eduard Grigoryev
Eduard Grigoryev

Reputation: 76

Change inner parts or the text inside a tag

I want to replace second_part or third_part with e.g. bar.

parser = etree.XMLParser(remove_comments=True, remove_blank_text=True)
root = etree.fromstring("<p>first_part<lb/>second_part<lb/>third_part</p>", parser=parser)

We can easily get and change first_part:

p.text
# returns 'first_part'

Also we can retrieve tail text with tags:

''.join(ET.tostring(e, 'unicode') for e in root)
# returns '<lb />second_part<lb />third_part'

But I want to change the text. How can I do it (without regex, only using XMLParser)?

Upvotes: 0

Views: 76

Answers (1)

mzjn
mzjn

Reputation: 51002

You'll have to update the tail property of the <lb> elements.

from lxml import etree

root = etree.fromstring("<p>first_part<lb/>second_part<lb/>third_part</p>")

lb_elements = root.findall("lb")
lb_elements[0].tail = "foo"
lb_elements[1].tail = "bar"

print(etree.tostring(root, encoding="unicode"))

Output:

<p>first_part<lb/>foo<lb/>bar</p>

Upvotes: 1

Related Questions