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:
break
else:
el = None
return el
RemoveElementWithText( table, 'td', 'sometext' )
My table is like so:
<table>
<thead>...
<tbody><tr><td>...
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.
Also note that there is an independent ElementTree implementation called
lxml.etree, which has parent pointers.
Stefan
Note that all-camel-case names are rather unusual for function names and
rather used for class names. See PEP 8 for a style guide.
> for el in topEl.getiterator( subEl ):
> if el.text = text:
> break
> else:
> el = None
> return el
>
> RemoveElementWithText( table, 'td', 'sometext' )
>
> My table is like so:
>
> <table>
> <thead>...
> <tbody><tr><td>...
>
> Is there any way to do this in ElementTree?
I think the easiest is to iterate not over the elements themselves, but
over their parents, and then to remove all children of the specified tag in
each step.
Stefan