我有下面的XML文件:
<annotation>
<folder>JPEGImages</folder>
<filename>01FQ0YY92XRX5MDWGYC2RJ1CP4.jpeg</filename>
<path>D:\aVisionData\PVL Pilot Project\test\Annotation\JPEGImages\01FQ0YY92XRX5MDWGYC2RJ1CP4.jpeg</path>
<source>
<database>Unknown</database>
</source>
<size>
<width>601</width>
<height>844</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<name>smallObject</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>329</xmin>
<ymin>199</ymin>
<xmax>376</xmax>
<ymax>242</ymax>
</bndbox>
</object>
</annotation>
我想删除<path>
,还想编辑<source> </source>
,如下所示
<annotation>
<folder>JPEGImages</folder>
<filename>01FQ0YY92XRX5MDWGYC2RJ1CP4.jpeg</filename>
<source>
<database>objects</database>
<annotation>custom</annotation>
<image>custom</image>
</source>
<size>
<width>601</width>
<height>844</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<name>smallObject</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>329</xmin>
<ymin>199</ymin>
<xmax>376</xmax>
<ymax>242</ymax>
</bndbox>
</object>
</annotation>
要删除<path>
,我使用了以下代码:
import xml.etree.ElementTree as Et
file_path = os.path.join(inputAnnotationPath, annotation)
tr = Et.parse(file_path)
for element in tr.iter():
for subElement in element:
print(subElement)
if subElement.tag == "path":
se = subElement.get("path")
element.remove(subElement)
tr.write(sys.stdout)
它运行正常,但无法删除path
。我应该做什么更改来删除path
并修改source
。
1条答案
按热度按时间cczfrluj1#
如果您可以使用lxml,这将非常简单: