Please help, Python beginner,
after getting all the data from xml, data_list = xmlTree.findall('.//data') e.g here I get 10 rows Now, I need to keep only a few rows for which attribute 'name' values match with elements of another list (inputID) with three IDs inside. e.g. remains only 3 rows whose name attribute match with the list elements
Thank you.
You can use a for loop to iterate over each element, then decide if each element should be removed. I used the Python doc ElementTree XML API for reference.
from xml.etree.ElementTree import ElementTree
tree = ElementTree()
# Test input
tree.parse("sample.xml")
# List containing names you want to keep
inputID = ['name1', 'name2', 'name3']
for node in tree.findall('.//data'):
# Remove node if the name attribute value is not in inputID
if not node.attrib.get('name') in inputID:
tree.getroot().remove(node)
# Do what you want with the modified xml
tree.write('sample_out.xml')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With