Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中使用xml树库从xml中删除元素_Python_Xml_Xml Parsing_Labelimg - Fatal编程技术网

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

在python中使用xml树库从xml中删除元素,python,xml,xml-parsing,labelimg,Python,Xml,Xml Parsing,Labelimg,我正在从事一个项目,在该项目中,我对某些树叶的图像进行了注释,并将其保存为xml格式,以便使用对象检测识别树叶上的害虫。 但是,由于一些害虫看起来相似,但实际上它们是不同的,所以我在某些对象中面临一些模糊性,所以我考虑删除一个类。由于我已经对所有图像进行了注释,手动删除标签是一项乏味的任务,因此我考虑编写一个脚本来删除xml文件中的那些对象。 该文件的结构是: <annotation> <folder>Set 3 A</folder> <filename

我正在从事一个项目,在该项目中,我对某些树叶的图像进行了注释,并将其保存为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>

第3组A
IMG-20200904-WA0105.jpg
C:\Users\Admin\Desktop\Set 3 A\Set 3 A\IMG-20200904-WA0105.jpg
不为人知
960
1280
3.
0
粉虱
未指明
0
0
232
83
286
173
Jassid攻击效应
未指明
0
0
356
7.
563
359
Jassid攻击效应
未指明
0
0
356
7.
563
359
粉虱
未指明
0
0
232
83
286
173
因此,如果我想删除对象名“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>

Jassid攻击效应
未指明
0
0
356
7.
563
359

尝试以下方法:

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())
stuff=r”““您的xml在”“之上”#您需要“r”,因为您有未替换的反斜杠;还要注意,xml的格式不正确;你漏掉了结束语
从lxml导入etree
doc=etree.XML(stuff)
target=doc.xpath('//object[name[“Jassid攻击效果”]]')[0]
target.getparent().remove(目标)
打印(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>

第3组A
IMG-20200904-WA0105.jpg
C:\Users\Admin\Desktop\Set 3 A\Set 3 A\IMG-20200904-WA0105.jpg
不为人知
960
1280
3.
0
粉虱
未指明
0
0
232
83
286
173

谢谢!此外,有些文件具有多次“Jassid攻击效果”,因此如何从文件中删除所有文件,而不仅仅是单个文件?以及如何保存对原始文件所做的更改?