有时候我们得到了一些数据,但是想放在一个标准xml的格式里,该如何做呢?
下面以目标检测标签为例,将数据写入xml文件中:
from lxml.etree import Element,SubElement,tostring
from xml.dom.minidom import parseString
import sys
savedStdout = sys.stdout
f = open('test.xml','w+')
sys.stdout = f
node_root = Element('annotation')
node_folder = SubElement(node_root,'floder')
node_folder.text = 'train'
node_filename = SubElement(node_root,'filename')
node_filename = '1.jpg'
node_size = SubElement(node_root,'size')
node_width = SubElement(node_size,'width')
node_width.text = '207'
node_height = SubElement(node_size,'height')
node_height.text = '243'
node_depth = SubElement(node_size,'depth')
node_depth.text = '3'
node_object = SubElement(node_root,'object')
node_name = SubElement(node_object,'name')
node_name.text = '01'
node_difficult = SubElement(node_object,'difficult')
node_difficult .text = '0'
node_bndbox = SubElement(node_object,'bndbox')
node_xmin = SubElement(node_bndbox,'xmin')
node_xmin.text = '0'
node_ymin = SubElement(node_bndbox,'ymin')
node_ymin.text = '24'
node_xmax = SubElement(node_bndbox,'xmax')
node_xmax.text = '231'
node_ymax = SubElement(node_bndbox,'ymax')
node_ymax.text = '199'
xml = tostring(node_root,pretty_print = True)
dom = parseString(xml)
print(xml)
f.close()
sys.stdout = savedStdout
网友评论