简体   繁体   English

在python中删除具有特定属性的子xml元素

[英]Remove child xml element with specific attribute in python

my first question 我的第一个问题

I am trying to clear up eshop database dump of products that either don't have price or quantity set. 我正在尝试清除没有设置价格或数量的产品的eshop数据库转储。 So I only get ready to sell products. 所以我只准备出售产品。 I am trying to do this via python script. 我正在尝试通过python脚本做到这一点。 After the script failed to do what I intended I tried making testing script. 在脚本无法完成我的预期之后,我尝试制作测试脚本。

input file test.xml 输入文件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: 而output.xml看起来像这样:

<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 1)删除正确的元素

2) Didn't remove correct element 2)没有删除正确的元素

and 3) Removed incorrect element 和3)删除了不正确的元素

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. 我也很乐意批评我的问题,我做错了什么,我可以做的更好。

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 看到这个答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM