如何利用代码将文件夹A内的若干个xml文档中前几个<object></object>部分删除仅保留最后一个<object></object>及其之内的内容
时间: 2023-12-08 15:06:10 浏览: 121
可以使用Python的ElementTree模块来解析xml文件,并且使用XPath选择器定位到要删除的<object></object>部分,然后使用remove()方法将其删除,最后将修改后的xml保存。以下是示例代码:
```python
import xml.etree.ElementTree as ET
import os
folder_path = 'path/to/folder/A'
for filename in os.listdir(folder_path):
if filename.endswith('.xml'):
xml_path = os.path.join(folder_path, filename)
tree = ET.parse(xml_path)
root = tree.getroot()
objects = root.findall('.//object')
# 删除前面的<object></object>部分
for obj in objects[:-1]:
root.remove(obj)
# 保存修改后的xml
tree.write(xml_path)
```
希望这能对你有所帮助!如果你有其它问题,请继续提出来。
相关问题
用python帮我把下面标签中的有效数据提取出来<annotation> <folder>converted/CMS/2D目标检测/filter</folder> <filename>converted/CMS/2D目标检测/filter_empty_target_img_after_hash2/0/20230401180910649_61.jpg</filename> <source> <database>Unknown</database> <annotation>Unknown</annotation> <image>Unknown</image> </source> <size> <width>1920</width> <height>1536</height> <depth></depth> </size> <segmented>0</segmented> <object> <name>二轮车</name> <truncated>0</truncated> <occluded>0</occluded> <difficult>0</difficult> <bndbox> <xmin>626.38</xmin> <ymin>808.12</ymin> <xmax>650.03</xmax> <ymax>852.04</ymax> </bndbox> <attributes> <attribute> <name>rotation</name> <value>0.0</value> </attribute> <attribute> <name>track_id</name> <value>6</value> </attribute> <attribute> <name>keyframe</name> <value>True</value> </attribute> </attributes> </object> <object> <name>行人</name> <truncated>0</truncated> <occluded>0</occluded> <difficult>0</difficult> <bndbox> <xmin>1612.74</xmin> <ymin>831.51</ymin> <xmax>1627.34</xmax> <ymax>873.8</ymax> </bndbox> <attributes> <attribute> <name>rotation</name> <value>0.0</value> </attribute> <attribute> <name>track_id</name> <value>8</value> </attribute> <attribute> <name>keyframe</name> <value>True</value> </attribute> </attributes> </object> <object> <name>行人</name> <truncated>0</truncated> <occluded>0</occluded> <difficult>0</difficult> <bndbox> <xmin>1469.0</xmin> <ymin>832.96</ymin> <xmax>1489.43</xmax> <ymax>865.8</ymax> </bndbox> <attributes> <attribute> <name>rotation</name> <value>0.0</value> </attribute> <attribute> <name>track_id</name> <value>9</value> </attribute> <attribute> <name>keyframe</name> <value>True</value> </attribute> </attributes> </object> </annotation>
你可以使用Python中的xml.etree.ElementTree库来解析XML数据并提取标签中的有效数据。以下是一个示例代码,演示如何提取出有效数据:
```python
import xml.etree.ElementTree as ET
# 定义XML字符串
xml_string = '''
<annotation>
<folder>converted/CMS/2D目标检测/filter</folder>
<filename>converted/CMS/2D目标检测/filter_empty_target_img_after_hash2/0/20230401180910649_61.jpg</filename>
<source>
<database>Unknown</database>
<annotation>Unknown</annotation>
<image>Unknown</image>
</source>
<size>
<width>1920</width>
<height>1536</height>
<depth></depth>
</size>
<segmented>0</segmented>
<object>
<name>二轮车</name>
<truncated>0</truncated>
<occluded>0</occluded>
<difficult>0</difficult>
<bndbox>
<xmin>626.38</xmin>
<ymin>808.12</ymin>
<xmax>650.03</xmax>
<ymax>852.04</ymax>
</bndbox>
<attributes>
<attribute>
<name>rotation</name>
<value>0.0</value>
</attribute>
<attribute>
<name>track_id</name>
<value>6</value>
</attribute>
<attribute>
<name>keyframe</name>
<value>True</value>
</attribute>
</attributes>
</object>
<object>
<name>行人</name>
<truncated>0</truncated>
<occluded>0</occluded>
<difficult>0</difficult>
<bndbox>
<xmin>1612.74</xmin>
<ymin>831.51</ymin>
<xmax>1627.34</xmax>
<ymax>873.8</ymax>
</bndbox>
<attributes>
<attribute>
<name>rotation</name>
<value>0.0</value>
</attribute>
<attribute>
<name>track_id</name>
<value>8</value>
</attribute>
<attribute>
<name>keyframe</name>
<value>True</value>
</attribute>
</attributes>
</object>
<object>
<name>行人</name>
<truncated>0</truncated>
<occluded>0</occluded>
<difficult>0</difficult>
<bndbox>
<xmin>1469.0</xmin>
<ymin>832.96</ymin>
<xmax>1489.43</xmax>
<ymax>865.8</ymax>
</bndbox>
<attributes>
<attribute>
<name>rotation</name>
<value>0.0</value>
</attribute>
<attribute>
<name>track_id</name>
<value>9</value>
</attribute>
<attribute>
<name>keyframe</name>
<value>True</value>
</attribute>
</attributes>
</object>
</annotation>
'''
# 解析XML数据
root = ET.fromstring(xml_string)
# 提取有效数据
folder = root.find('folder').text
filename = root.find('filename').text
width = int(root.find('size/width').text)
height = int(root.find('size/height').text)
objects = []
for obj in root.findall('object'):
name = obj.find('name').text
xmin = float(obj.find('bndbox/xmin').text)
ymin = float(obj.find('bndbox/ymin').text)
xmax = float(obj.find('bndbox/xmax').text)
ymax = float(obj.find('bndbox/ymax').text)
objects.append({
'name': name,
'xmin': xmin,
'ymin': ymin,
'xmax': xmax,
'ymax': ymax
})
# 打印提取的有效数据
print("Folder:", folder)
print("Filename:", filename)
print("Width:", width)
print("Height:", height)
print("Objects:")
for obj in objects:
print(obj)
```
运行这段代码,它将解析XML字符串并提取出标签中的有效数据。你可以根据需要对提取出的数据进行进一步处理或存储。
xml标签文件的格式为 <annotation> <folder>JPEGImages</folder> <filename>VOC2007/JPEGImages/000000000049.jpg</filename> <size> <width>381</width> <height>500</height> <depth>3</depth> </size> <object> <name>person</name> <difficult>0</difficult> <bndbox> <xmin>204</xmin> <ymin>261</ymin> <xmax>269</xmax> <ymax>334</ymax> </bndbox> <pose>Unspecified</pose> <truncated>0</truncated> </object> <object> <name>person</name> <difficult>0</difficult> <bndbox> <xmin>119</xmin> <ymin>262</ymin> <xmax>175</xmax> <ymax>324</ymax> </bndbox> <pose>Unspecified</pose> <truncated>0</truncated> </object> <object> <name>person</name> <difficult>0</difficult> <bndbox> <xmin>120</xmin> <ymin>335</ymin> <xmax>131</xmax> <ymax>368</ymax> </bndbox> <pose>Unspecified</pose> <truncated>0</truncated> </object> <object> <name>person</name> <difficult>0</difficult> <bndbox> <xmin>285</xmin> <ymin>334</ymin> <xmax>295</xmax> <ymax>361</ymax> </bndbox> <pose>Unspecified</pose> <truncated>0</truncated> </object> <object> <name>person</name> <difficult>0</difficult> <bndbox> <xmin>192</xmin> <ymin>335</ymin> <xmax>199</xmax> <ymax>358</ymax> </bndbox> <pose>Unspecified</pose> <truncated>0</truncated> </object> <object> <name>person</name> <difficult>0</difficult> <bndbox> <xmin>347</xmin> <ymin>334</ymin> <xmax>361</xmax> <ymax>343</ymax> </bndbox> <pose>Unspecified</pose> <truncated>0</truncated> </object> </annotation>
根据你提供的XML标签文件的格式,每个标注包含以下信息:
- 文件夹名称(`<folder>`)
- 文件名(`<filename>`)
- 图像尺寸(`<size>`)包括宽度(`<width>`)、高度(`<height>`)和深度(`<depth>`)
- 物体信息(`<object>`)包括类别名称(`<name>`)、困难度(`<difficult>`)、边界框坐标(`<bndbox>`)和其他属性(如姿势和截断状态)
每个物体信息都有一个单独的`<object>`标签,并在其中包含上述子标签。
如果你有任何特定的问题或需要进一步的帮助,请告诉我。
阅读全文