python 常用包之xml文件处理
1,处理xml的包
from xml.etree import ElementTree as ET
2,如何写出xml文件
xml文件和html中的元素很像,有父级子集之说,
root = ET.Element('opencv_storage') #创建根元素,根元素的变量名叫root,元素名字叫opencv_storage person1 = ET.SubElement(root, 'cameraId') #在根元素上创建子元素,子元素的变量名叫person1,元素名叫cameraID person1.text="camera url" #persion1对应的元素内容是"camera url" intr = ET.SubElement(root, 'intrinsic_parameters', {'type_id':'opencv-matrix'}) #在根元素上再加一个子元素叫intr,元素有个属性是“type_id",属性的内容是'opencv-matrix' rows = ET.SubElement(intr, 'rows') #intr元素上加子元素,以下同理 rows.text="3" cols=ET.SubElement(intr, 'cols') cols.text="3" dt=ET.SubElement(intr, 'dt') dt.text="d" data=ET.SubElement(intr, 'data') data.text=" 1 2 3 1 2 3 1 2 3 " dist=ET.SubElement(root, 'distortion_parametes',{"type_id":"opencv-matrix"}) rows2=ET.SubElement(dist, 'rows') rows2.text="1" cols2=ET.SubElement(dist, 'cols') cols2.text="5" dt2=ET.SubElement(dist, 'dt') dt2.text='d' data2=ET.SubElement(dist, 'data') data2.text="1 2 3 4 5" tree = ET.ElementTree(root)#将根目录转化为xml树状结构(即ElementTree对象) ET.dump(root)#在终端显示整个xml内容 tree.write('sample.xml', encoding="utf-8", xml_declaration=True)#写入xml文件
sample.xml的文件内容:
<?xml version='1.0' encoding='utf-8'?> <opencv_storage> <cameraId>camera url</cameraId> <intrinsic_parameters type_id="opencv-matrix"> <rows>3</rows> <cols>3</cols> <dt>d</dt> <data>1 2 3 1 2 3 1 2 3 </data> </intrinsic_parameters> <distortion_parametes type_id="opencv-matrix"> <rows>1</rows> <cols>5</cols> <dt>d</dt> <data>1 2 3 4 5</data> </distortion_parametes> </opencv_storage>
3,如何解析xml文件
以解析上一个sample.xml为例:
try: tree = ET.parse("./sample.xml”) root = tree.getroot() except Exception as e: print("xml文件打开错误",e) return -1 for i in root: #可以直接遍历父元素来获取子元素 if i.tag == "intrinsic_parameters": #i.tag是元素名 matrix_height=int(i.find("rows").text) #i.find("rows")获取子元素中名字叫rows的元素,不会递归查找,如有多个只获取第一个。findall可以获取多个,i.text()获取某元素的内容 matrix_width=int(i.find("cols").text) matrix_str=i.find("data").text if i.tag == "distortion_parametes": coefs_height=int(i.find("rows").text) coefs_width=int(i.find("cols").text) coefs_str=i.find("data").text #获取到元素内容以后可以自行解析
补充:
root.attrib:获取属性
Element.get():访问标签的属性值
Element.set():添加和修改标签的属性和属性值。
Element.append():添加孩子节点
4,拓展
这样的xml:sample.xml
<?xml version="1.0"?> <actors xmlns:fictional="http://characters.example.com" xmlns="http://people.example.com"> <actor> <name>John Cleese</name> <fictional:character>Lancelot</fictional:character> <fictional:character>Archie Leach</fictional:character> </actor> <actor> <name>Eric Idle</name> <fictional:character>Sir Robin</fictional:character> <fictional:character>Gunther</fictional:character> <fictional:character>Commander Clement</fictional:character> </actor> </actors>
通过这个操作:
from xml.etree import ElementTree as ET
tree = ET.parse('sample.xml')
root = tree.getroot()
ns = {'real_person': 'http://people.example.com',
'role': 'http://characters.example.com'}
for actor in root.findall('real_person:actor', ns):
name = actor.find('real_person:name', ns)
print(name.text)
for char in actor.findall('role:character', ns):
print(' |-->', char.text)
生成如下代码:(还没看懂)
John Cleese
|--> Lancelot
|--> Archie Leach
Eric Idle
|--> Sir Robin
|--> Gunther
|--> Commander Clement
5,opencv自带的xml处理器
opencv自带一个文件处理器,名字叫cv2.FileStorage,用法如下:
import cv2 import numpy as np #从xml文件中读出 aa=cv2.FileStorage("./hehe.xml",cv2.FileStorage_READ) print(aa.getNode("intrinsic_parameters").mat()) #写入xml文件 fs = cv2.FileStorage('./hehe.xml', cv2.FileStorage_WRITE) aa=range(9) bb=range(5) camera_matrix=np.array(aa).reshape(3,3) dist_coefs=np.array(bb).reshape(1,5) fs.write("intrinsic_parameters", camera_matrix) fs.write("distortion_parametes", dist_coefs)