前言:本次项目需要对xml进行信息读取以便可视化数据分布,这里简单介绍一下python中xml.ttree.ElementTree包
data
000001.jpg
D:\Program Files\Profiles\labelimg\data\000001.jpg
Unknown
1500
1000
3
0
---xml解析:
tree = ET.parse("你的xml路径") #得到ET对象
root = tree.getroot() #得到根节点
ET.dump(root) #显示整个xml
---对于每一个element对象都有一下属性:
tag: string对象, 表示数据代表的种类
attrib:dictionary对象, 表示附有的属性
text: string对象, 表示element的内容
tail:string对象,表示element闭合之后的尾迹
若干子元素
import os
import xml.etree.cElementTree as ET
import shutil
import sys
tree = ET.parse("000001.xml")
root = tree.getroot()
print("root:", root)
print("tag:",root.tag)
print("attrib",root.attrib)
print("text", root.text)
print("tail:",root.tail)
'''
root:
tag: annotation
attrib {}
text '\n'
tail: None'''
---简单遍历:
#直接全遍历
for child in root:
print("tag:", child.tag, "attrib:", child.attrib, "text:", child.text)
'''
tag: folder attrib: {} text: data
tag: filename attrib: {} text: 000001.jpg
tag: path attrib: {} text: D:\Program Files\Profiles\labelimg\data\000001.jpg
tag: source attrib: {} text:
tag: size attrib: {} text:
tag: segmented attrib: {} text: 0
tag: object attrib: {} text:
tag: object attrib: {} text:
tag: object attrib: {} text:
tag: object attrib: {} text:
'''
#数组的形式访问
print(root[4][1].tag)#height
---一些方便的查找函数:
1.find(match) #查找第一个匹配的子元素,match可以是tag或是xpath路径
2.findall(match) #返回所有匹配的子元素列表
3.findtext(match , default=None)
4.iter(tag=None) #以当前元素为根节点,创建树迭代器,如果tag不是none,则以tag过滤
5.iterfind(match) #
for child in root.iter("name"):#不只下一级
print(child.text)
for child in root.findall("object"):#只能找下一级
print(child.text)
----修改xml
1.属性相关(是一个标签里的属性级操作)
改好后:tree.write("你保存的xml路径") #保存
keys() 返回元素属性名称列表
get
(key, default=None) 获取属性set
(key, value) # 跟新/添加 属性2.节点相关
删除节点:.remove(....)
添加子元素方法总结:
append
(subelement) extend
(subelements)insert
(index, element)