[Tutor] removing nodes using ElementTree
Alan Gauld
alan.gauld at btinternet.com
Mon Jun 29 02:02:56 CEST 2015
On 27/06/15 01:03, street.sweeper at mailworks.org wrote:
> I'm trying to merge and filter some xml. This is working well, but I'm
> getting one node that's not in my list to include.
This may be similar to a recent post with similar issues.
> for rec in xmlet:
> if rec.find('part').find('pid').text not in il:
> xmlet.remove(rec)
You're removing records from the thing you are iterating over.
That's never a good idea, and often causes the next record to
be stepped over without being processed.
Try taking a copy before iterating and then modify the original.
for rec in xmlet[:]: # slice creates a copy
if rec....
xmlet.remove(rec)
HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list