示例代码说明:
读取txt指定内容如地物id,输出OpenDRIVE数据中的对应地物id和名称name信息为xml文件
import xml.dom.minidom from xml.dom.minidom import parse from xml.dom import Node import sys import os # 读取OpenDRIVE文件路径 xml_filepath = r'D:\OpenDRIVE.xodr' # 得到文件对象 dom_obj = xml.dom.minidom.parse(xml_filepath) #得到xml文档对象 root = dom_obj.documentElement filepath = "D:\\objectname.xml" if (os.path.exists(filepath)):os.remove(filepath) fp = open(filepath, 'a+') objects = root.getElementsByTagName('object') objectpath = "D:/objects.txt" fobject = open(objectpath,'r') lines = fobject.readlines() for line in lines:baseID = line.split(' ')[0].split(':')[1]#print(baseID)for object in objects:if object.getAttribute("id") == baseID:featuresid = object.getAttribute("id")featuresname = object.getAttribute("name")print("地物ID:",featuresid)print("地物name:", featuresname)fp.write(featuresid)fp.write(" ")fp.write(featuresname)fp.write("\n") fp.close()
objects.txt文件示例:
xml输出结果示例: