#!/usr/bin/python # -*- coding: UTF-8 -*- from xml.dom.minidom import parse import xml.dom.minidom """ 数据输入 xmlname 文件名字 nodename 要获取的路径 输入格式 <?xml version="1.0"?> <opencv_storage> <KfrmGPS_ type_id="opencv-matrix"> <rows>2508</rows> <cols>3</cols> <dt>d</dt> <data> 3.4236497333300001e+01 1.0889943616700000e+02 400. 3.4236502333300002e+01 1.0889943933300000e+02 400. 3.4236507166700001e+01 1.0889944250000001e+02 400. 3.4236511999999998e+01 1.0889944566699999e+02 400. </data> </KfrmGPS_> <KfrmGPS_m_ type_id="opencv-matrix"> <rows>2508</rows> <cols>3</cols> <dt>d</dt> <data> 3.4236724219391739e+01 1.0889944810627938e+02 3.9653005784470588e+02 3.4236728962259967e+01 1.0889945157710224e+02 3.9650777322053909e+02 3.4236731965099686e+01 1.0889945368682891e+02 3.9653498737327754e+02 3.4236738454649888e+01 1.0889945827032633e+02 3.9652323448006064e+02 </data> </KfrmGPS_m_> </opencv_storage> 输出 转化成flaost的列表 纬 经 高 """ def read_xml_getgps(xmlname,nodename): # 使用minidom解析器打开 XML 文档 DOMTree = xml.dom.minidom.parse(xmlname) collection = DOMTree.documentElement KfrmGPS_ = collection.getElementsByTagName(nodename) #for KfrmGPS_i in KfrmGPS_: KfrmGPS_i=KfrmGPS_[0]#可能有多个 gps_rows = KfrmGPS_i.getElementsByTagName('rows')[0] gps_rows = gps_rows.childNodes[0].data gps_cols = KfrmGPS_i.getElementsByTagName('cols')[0] gps_cols = gps_cols.childNodes[0].data gps_data = KfrmGPS_i.getElementsByTagName('data')[0] gps_data_str=str(gps_data.childNodes[0].data) #print("KfrmGPS_/data/内容",gps_data_str) alllines=gps_data_str.strip().split("\n")# 行的分割符号 两个空格 i=1 gps_list=[] for lines_i in alllines: line_ii=lines_i.strip().split(" ") # 行内部的珊瑚橘分割符号 一个空格 real_gps_lat = line_ii[0:1] # list--str real_gps_lat="".join(real_gps_lat).strip() real_gps_lon = line_ii[1:2] # list--str real_gps_lon="".join(real_gps_lon).strip() real_gps_high = line_ii[2:3] # list--str real_gps_high="".join(real_gps_high).strip() #print("字符串类型",i,real_gps_lon,real_gps_lat,real_gps_high) real_gps_lat=float(real_gps_lat) real_gps_lon=float(real_gps_lon) real_gps_high=float(real_gps_high) #print("float类型",i,real_gps_lon,real_gps_lat,real_gps_high) i=i+1 gps_i=[real_gps_lat,real_gps_lon,real_gps_high] gps_list.append(gps_i) print("节点",nodename,"行数",gps_rows,"列数",gps_cols) return gps_list if ( __name__ == "__main__"): xmlpath="20230107142004075.xml" real_gps_list=read_xml_getgps(xmlpath,"KfrmGPS_") slam_gps_list=read_xml_getgps(xmlpath,"KfrmGPS_m_")
标签:xml,real,KfrmGPS,读取,02,data,gps From: https://www.cnblogs.com/gooutlook/p/17086798.html