Skip to content

Instantly share code, notes, and snippets.

@iaverypadberg
Last active April 26, 2022 13:50
Show Gist options
  • Save iaverypadberg/5e9f89e126ee41977eb93f5b7c9c831b to your computer and use it in GitHub Desktop.
Save iaverypadberg/5e9f89e126ee41977eb93f5b7c9c831b to your computer and use it in GitHub Desktop.
Remove annotation label from an xml file
import xml.etree.ElementTree as ET
from glob import glob
labels = [
"labels",
"to",
"be",
"removed"]
files = glob('directory/with/xml/files/*.xml')
for file in files:
tree = ET.parse(file)
root = tree.getroot()
objects = root.findall('object')
changed = False
for object in objects:
label = object.find('name').text
if(label in labels):
root.remove(object)
changed=True
if changed:
tree.write(file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment