Frage

I read here that in order to replace an element text, I would just have to use replace. For some reason in my code it throws this error:

AttributeError: 'lxml.etree._ElementTree' object has no attribute 'replace'

Here is the code:

from lxml import etree

old = 'Hello'
new = 'bye'

xml.replace(currentText, newText)

The code above is simplified. What I want is to replace one tag text with another text.

War es hilfreich?

Lösung

No, no. _ElementTree.replace actualy replaces subelement with element passed as second argument. All you need to is:

xml.text = new_text

By the way, http://lxml.de can be wery useful, it have a lot of information and code examples for such cases.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top