
Python读xml
Python读xml
之前用过c++读xml,太困难了,可以参考这篇文章c++读xml,python就相对容易些。 python读xml的方法比较多,下面就介绍两种。 1、xml.dom.minidom
import glob import xml.dom.minidom for xmlPath in glob.glob("../asset/xml" + "/*.xml"): print(xmlPath) dom = xml.dom.minidom.parse(xmlPath) root = dom.documentElement itemList = root.getElementsByTagName('data') ## 内参 data = itemList[0].firstChild.data print(data)代码会循环读取项目根目录下/asset/xml文件夹里面所有的类型为xml的文件。 这样可以依据getElementsByTagName得到整个xml中标签为data的值,但是这样读出来的对象都是str类型的,遇到读opencv矩阵就很麻烦,所以可以用opencv来读 xml中矩阵如下
<mat type_id="opencv-matrix"> <rows>3</rows> <cols>3</cols> <dt>d</dt> <data> 7.6771045506683436e+02 0. 3.0315526341908003e+02 0. 7.6771045506683436e+02 2.4620553442166459e+02 0. 0. 1.</data></mat> <distCoeff type_id="opencv-matrix">2、opencv
for xmlPath in glob.glob("../asset/xml" + "/*.xml"): print(xmlPath) cv_file = cv2.FileStorage(xmlPath, cv2.FILE_STORAGE_READ) matrix = cv_file.getNode("mat").mat() print("read matrix\\n", matrix) cv_file.release()这样得到的matrix 就是矩阵,就可以用下标来访问了。
👁️ 阅读量:0
© 版权声明:本文《Python读xml》内容均为本站精心整理或网友自愿分享,如需转载请注明原文出处:https://www.zastudy.cn/wen/1687006835a416860.html。