0

my first question

I am trying to clear up eshop database dump of products that either don't have price or quantity set. So I only get ready to sell products. I am trying to do this via python script. After the script failed to do what I intended I tried making testing script.

input file test.xml

<Result >
 <StoItem Code="A" Id="1" QtyFree="2" PriceEU="124.5">
  <ImgGal />
 </StoItem>  
 <StoItem Code="B" Id="2" QtyFree="2" PriceEU="124.5">
  <ImgGal />
 </StoItem>
 <StoItem Code="C" Id="3" PriceEU="124.5">
  <ImgGal />
 </StoItem>
 <StoItem Code="D" Id="4" QtyFree="2" >
  <ImgGal />
 </StoItem>
</Result>

Now my script looks like this:

import xml.etree.ElementTree as ET
tree = ET.parse('test.xml')
root = tree.getroot()
atb='QtyFree'
for child in root:
    print('Checking element no. '+child.attrib['Id'])
        if atb in child.attrib:
             print('In '+child.attrib['Id']+' found '+atb)
             print('deleted '+child.attrib['Id'] )
        else:
             print('In '+child.attrib['Id']+'not found '+atb)
tree.write('output.xml')

Now the output correctly identified elements which should be deleted as:

Checking element no. 1
In 1 found QtyFree
deleted 1
Checking element no. 2 
In 2 found QtyFree
deleted 2
Checking element no. 3
In 3not found QtyFree  
Checking element no. 4 
In 4 found QtyFree 
deleted 4    

But when I put the actual removing function in the script:

if atb in child.attrib:
    print('In '+child.attrib['Id']+' found '+atb)
    root.remove(child)
    print('deleted '+child.attrib['Id'] )

I get something like this:

Checking element no. 1
In 1 found QtyFree
deleted 1
Checking element no. 3
In 3not found QtyFree
Checking element no. 4
In 4 found QtyFree
deleted 4  

And output.xml looks like this:

<Result>
 <StoItem Code="B" Id="2" PriceEU="124.5" QtyFree="2">
  <ImgGal />
 </StoItem>
 <StoItem Code="D" Id="4" QtyFree="2">
  <ImgGal />
 </StoItem>
</Result>

Meaning, that it

1) Removed correct element

2) Didn't remove correct element

and 3) Removed incorrect element

So if anyone knows what and where the bug is, I would be really happy Thank you for your time. I am also open to critic of my question and what did I do wrong and what I could do better.

Budbreaker
  • 35
  • 5

1 Answers1

0

The problem is that you modify the tree while you iterate over it.

Instead of

for child in root:

use

for child in tree.findall('.//StoItem'):

See this answer

Community
  • 1
  • 1
Daniel Hepper
  • 28,981
  • 10
  • 72
  • 75