python解析xml文件增删查找
XML文件
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/88970fbb9cce947da170a10814e0092d.png)
判断bndbox大小,并为object添加子节点difficult
import os
import os.path
from xml.etree import ElementTree as ET
path='C:/Users/my/Desktop/xmllll/city/1'
new_path='C:/Users/my/Desktop/xmllll/city/1/'
files=os.listdir(path)
my_area = int(input("请输入area区域大小:"))
for xmlFile in files:
if not os.path.isdir(xmlFile):
print(xmlFile)
per = ET.parse(new_path + xmlFile)
p=per.findall('./object')
number_name = 0
area = []
for oneper in p:
for child in oneper.getchildren():
for grandson in child.getchildren():
area.append(int(child.find('xmin').text))
area.append(int(child.find('ymin').text))
area.append(int(child.find('xmax').text))
area.append(int(child.find('ymax').text))
if len(area) == 4:
element = ET.Element("difficult")
if ((area[2]-area[0]) > my_area) & ((area[3]-area[1])> my_area):
element.text = "0"
else:
element.text = "1"
oneper.insert(1,element)
per.write(new_path + xmlFile, encoding="utf-8",xml_declaration=True)
area.clear()
break
print('-------')
- 该文件可以批处理xml文件
- 可以添加子节点,索引
- 但是存在速度问题,一直没法提高速度,应该和使用了大量for循环有关
- 注意:修改的xml文件是缓存在内存当中的,因此一定要保存
- 添加子节点可以用append或者insert,指定位置用insert
Result
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/73b4aa1cf8a7411be47527fd5c24e4fd.png)
xml增删查找
"""
Created on Mon Mar 18 17:36:45 2019
@author: psqk
"""
from xml.etree import ElementTree
from xml.etree.ElementTree import Element, SubElement
from lxml import etree
import codecs
XML_EXT = '.xml'
ENCODE_METHOD = 'utf-8'
class PascalVocWriter:
def __init__(self, foldername, filename, imgSize,databaseSrc='Unknown', localImgPath=None):
self.foldername = foldername
self.filename = filename
self.databaseSrc = databaseSrc
self.imgSize = imgSize
self.boxlist = []
self.localImgPath = localImgPath
self.verified = False
def prettify(self, elem):
"""
Return a pretty-printed XML string for the Element.
"""
rough_string = ElementTree.tostring(elem, 'utf8')
root