读取xml文件的方法
import os
import xml.etree.ElementTree as et
def re_xml(path):
xml_path = os.path.join(data, path)
tree = et.parse(xml_path)
root = tree.getroot()
# 单独修改内容
root.find('name').find('width').text = '1024'
# 批量修改内容
# 注意:有时iter()不能遍历全部节点,使用 “findall” 即可
for ob in root.iter('object'):
if ob.find('name').text == 'a':
ob.find('name').text == 'b'
tree.write(os.path.join(save, path))
删除某些节点
xml_path = os.path.join(data, path)
tree = et.parse(xml_path)
root = tree.getroot()
for i in root.findall("object"):
if 条件:
root.remove(ob)