微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用python中的xml树库从xml中删除元素

如何解决使用python中的xml树库从xml中删除元素

我正在开展一个项目,在该项目中,我对某些叶子的图像进行了注释,并将它们保存为 xml 格式,以便使用对象检测来识别叶子上的害虫。 但是由于我在某些对象上面临一些歧义,因为有些害虫看起来相似但实际上它们不同,所以我想删除一个类。而且由于我已经对所有图像进行了注释,手动删除标签是一项繁琐的任务,因此我想到了编写一个脚本来删除 xml 文件中的这些对象。 文件结构为:

<annotation>
<folder>Set 3 A</folder>
<filename>IMG-20200904-WA0105.jpg</filename>
<path>C:\Users\Admin\Desktop\Set 3 A\Set 3 A\IMG-20200904-WA0105.jpg</path>
<source>
    <database>UnkNown</database>
</source>
<size>
    <width>960</width>
    <height>1280</height>
    <depth>3</depth>
</size>
<segmented>0</segmented>
<object>
    <name>Whiteflies</name>
    <pose>Unspecified</pose>
    <truncated>0</truncated>
    <difficult>0</difficult>
    <bndBox>
        <xmin>232</xmin>
        <ymin>83</ymin>
        <xmax>286</xmax>
        <ymax>173</ymax>
    </bndBox>
</object>
<object>
    <name>Jassid Attack Effect</name>
    <pose>Unspecified</pose>
    <truncated>0</truncated>
    <difficult>0</difficult>
    <bndBox>
        <xmin>356</xmin>
        <ymin>7</ymin>
        <xmax>563</xmax>
        <ymax>359</ymax>
    </bndBox>
</object>
<object>
    <name>Jassid Attack Effect</name>
    <pose>Unspecified</pose>
    <truncated>0</truncated>
    <difficult>0</difficult>
    <bndBox>
        <xmin>356</xmin>
        <ymin>7</ymin>
        <xmax>563</xmax>
        <ymax>359</ymax>
    </bndBox>
</object>
<object>
    <name>Whiteflies</name>
    <pose>Unspecified</pose>
    <truncated>0</truncated>
    <difficult>0</difficult>
    <bndBox>
        <xmin>232</xmin>
        <ymin>83</ymin>
        <xmax>286</xmax>
        <ymax>173</ymax>
    </bndBox>
</object>

所以,如果我想删除对象名称“Jassid Attack Effect”(它可能在一个文档中多次出现,并且必须按照上面的xml代码所示将它们全部删除)及其内容,我将如何去做?例如:在解析时,对象名称是“Jassid Attack Effect”,然后我想从 xml 文件中完全删除它:

<object>
    <name>Jassid Attack Effect</name>
    <pose>Unspecified</pose>
    <truncated>0</truncated>
    <difficult>0</difficult>
    <bndBox>
        <xmin>356</xmin>
        <ymin>7</ymin>
        <xmax>563</xmax>
        <ymax>359</ymax>
    </bndBox>
</object>

解决方法

尝试这样的事情:

stuff = r"""your xml above""" #you need the "r" because you have unescaped backslashes; also note that the xml is not well-formed; you left out the closing <annotation> tag

from lxml import etree
doc = etree.XML(stuff)
target = doc.xpath('//object[name["Jassid Attack Effect"]]')[0]
target.getparent().remove(target)
print(etree.tostring(doc).decode())

输出:

<annotation>
<folder>Set 3 A</folder>
<filename>IMG-20200904-WA0105.jpg</filename>
<path>C:\Users\Admin\Desktop\Set 3 A\Set 3 A\IMG-20200904-WA0105.jpg</path>

<source><database>Unknown</database></source>
<size>
  <width>960</width>
    <height>1280</height>
    <depth>3</depth>
</size>
<segmented>0</segmented>
<object>
    <name>Whiteflies</name>
    <pose>Unspecified</pose>
    <truncated>0</truncated>
    <difficult>0</difficult>
    <bndbox>
        <xmin>232</xmin>
        <ymin>83</ymin>
        <xmax>286</xmax>
        <ymax>173</ymax>
    </bndbox>
</object>
</annotation>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。