我的第一个问题
我正试图清理eshop数据库中的产品,要么没有价格或数量设置。所以我只准备销售产品。我试图通过python脚本来实现这一点。 在脚本未能达到我的预期后,我尝试制作测试脚本。在
输入文件测试.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>
现在我的脚本如下:
^{pr2}$现在,输出正确识别了应删除的元素,如下所示:
^{3}$但当我在脚本中加入实际的移除函数时:
if atb in child.attrib:
print('In '+child.attrib['Id']+' found '+atb)
root.remove(child)
print('deleted '+child.attrib['Id'] )
我得到了这样的结论:
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
以及输出.xml看起来像这样:
<Result>
<StoItem Code="B" Id="2" PriceEU="124.5" QtyFree="2">
<ImgGal />
</StoItem>
<StoItem Code="D" Id="4" QtyFree="2">
<ImgGal />
</StoItem>
</Result>
意思是,它
1)移除正确的元件
2)没有移除正确的元件
以及3)移除了不正确的元件
所以,如果有人知道什么是窃听器,我会很高兴的 谢谢你抽出时间。我也愿意接受批评我的问题,我做错了什么,我可以做得更好。在
问题是在迭代树时修改树。在
而不是
使用
^{pr2}$看这个answer
相关问题 更多 >
编程相关推荐