March 3, 2015
9:23 a.m.
Hello, given the following piece of code which replaces all A nodes in P with B:
tree = etree.fromstring("<P><A/><A/></P>") l = tree.findall("A") first, last = tree.index(l[0]), tree.index(l[-1]) tree[first : last + 1] = [ etree.Element("B") ] etree.tostring(tree) '<P><B/></P>'
This code works, but it feels kind of ugly. Is there a more elegant way to replace a slice of the list of children (actually all children having the same name, but they always appear in a row) with another list of elements and keeping the document order intact? TIA, Markus
4237
Age (days ago)
4237
Last active (days ago)
0 comments
1 participants
participants (1)
-
Markus Schöpflin