remove element with ElementTree

tdan df.trash at
Tue Mar 9 04:35:50 CET 2010

I have been using ElementTree to write an app, and would like to
simply remove an element.
But in ElementTree, you must know both the parent and the child
element to do this.
There is no getparent() function, so I am stuck if I only have an

I am iterating over a table and getting all <td> tags, checking their
text, and conditionally deleting them:

def RemoveElementWithText( topEl, subEl, text ):
	for el in topEl.getiterator( subEl ):
		if el.text = text:
		el = None
	return el

RemoveElementWithText( table, 'td', 'sometext' )

My table is like so:


Is there any way to do this in ElementTree?  I see lxml.etree does
this nicely, but I want to use python's standard library.

More information about the Python-list mailing list