Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter xml data in Python

Tags:

python

xml

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.

like image 401
Indigo Avatar asked Mar 24 '26 12:03

Indigo


1 Answers

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')
like image 78
ryucl0ud Avatar answered Mar 26 '26 00:03

ryucl0ud