Python: Why this error is coming?



With reference to this question: Python: In an xml, How to delete nodes within a parent node


Can anyone explain me why I'm facing this issue in my python script.



Value Error : list.remove(x): x not in list


Code below.



import xml.etree.cElementTree as ET

try:
tree = ET.parse('Test.xml')
root = tree.getroot()
keeper_data = ['06354237', '87654321']
instances = root.findall('./Replication/Instance')
for instance in instances:
data = instance.find('./DataSet/Data')
if data.text not in keeper_data:
root.remove(instance)

tree.write('New.xml')

except ValueError as err:
print ('Value Error : ' + str(err))


xml sample below.Please note the only difference in xml structure from previous question is the addition of tag which encloses all the tags.



<?xml version='1.0' encoding='UTF-8'?>
<Root>
<Identification>
<Description ID="12">Some text</Description>
</Identification>
<Symbols>
<Name Width="1">abc</Name>
<Name Width="2">def</Name>
</Symbols>
<Replication iRowRef="884">
<Instance RowRef="A">
<DataSet>
<Data>12345678</Data>
</DataSet>
<DataSet>
<Data>abcd</Data>
</DataSet>
<DataSet>
<Data>abcd</Data>
</DataSet>
</Instance>
<Instance RowRef="B">
<DataSet>
<Data>87654321</Data>
</DataSet>
<DataSet>
<Data>abcd</Data>
</DataSet>
<DataSet>
<Data>abcd</Data>
</DataSet>
</Instance>
<Instance RowRef="C">
<DataSet>
<Data>06354237</Data>
</DataSet>
<DataSet>
<Data>abcd</Data>
</DataSet>
<DataSet>
<Data>abcd</Data>
</DataSet>
</Instance>
</Replication>
</Root>

No comments:

Post a Comment