python利用lxml读写xml格式的文件

作者:Arkenstone 时间:2023-08-02 04:22:44 

之前在转换数据集格式的时候需要将json转换到xml文件,用lxml包进行操作非常方便。

1. 写xml文件

a) 用etree和objectify


from lxml import etree, objectify

E = objectify.ElementMaker(annotate=False)
anno_tree = E.annotation(
 E.folder('VOC2014_instance'),
 E.filename("test.jpg"),
 E.source(
   E.database('COCO'),
   E.annotation('COCO'),
   E.image('COCO'),
   E.url("http://test.jpg")
 ),
 E.size(
   E.width(800),
   E.height(600),
   E.depth(3)
 ),
 E.segmented(0),
)

etree.ElementTree(anno_tree).write("text.xml", pretty_print=True)

输出的test.xml文件内容如下:

```

如果需要在anno_tree的基础上加其他标签的话用append即可:


E2 = objectify.ElementMaker(annotate=False)
anno_tree2 = E2.object(
 E.name("person"),
 E.bndbox(
   E.xmin(100),
   E.ymin(200),
   E.xmax(300),
   E.ymax(400)
 ),
 E.difficult(0)
)
anno_tree.append(anno_tree2)

上面的输出就变成了:


<annotation>
<folder>VOC2014_instance/person</folder>
<filename>test.jpg</filename>
<source>
 <database>COCO</database>
 <annotation>COCO</annotation>
 <image>COCO</image>
 <url>http://test.jpg</url>
</source>
<size>
 <width>800</width>
 <height>600</height>
 <depth>3</depth>
</size>
<segmented>0</segmented>
<object>
 <name>person</name>
 <bndbox>
  <xmin>100</xmin>
  <ymin>200</ymin>
  <xmax>300</xmax>
  <ymax>400</ymax>
 </bndbox>
 <difficult>0</difficult>
</object>
</annotation>

b) 用etree和SubElement


annotation = etree.Element("annotation")
etree.SubElement(annotation, "folder").text = "VOC2014_instance"
etree.SubElement(annotation, "filename").text = "test.jpg"
source = etree.SubElement(annotation, "source")
etree.SubElement(source, "database").text = "COCO"
etree.SubElement(source, "annotation").text = "COCO"
etree.SubElement(source, "image").text = "COCO"
etree.SubElement(source, "url").text = "http://test.jpg"
size = etree.SubElement(annotation, "size")
etree.SubElement(size, "width").text ='800' # 必须用string
etree.SubElement(size, "height").text = '600'
etree.SubElement(size, "depth").text = '3'
etree.SubElement(annotation, "segmented").text = '0'
key_object = etree.SubElement(annotation, "object")
etree.SubElement(key_object, "name").text = “person”
bndbox = etree.SubElement(key_object, "bndbox")
etree.SubElement(bndbox, "xmin").text = str(100)
etree.SubElement(bndbox, "ymin").text = str(200)
etree.SubElement(bndbox, "xmax").text = str(300)
etree.SubElement(bndbox, "ymax").text = str(400)
etree.SubElement(key_object, "difficult").text = '0'
doc = etree.ElementTree(annotation)
doc.write(open("test.xml", "w"), pretty_print=True)

2. 读xml

这里可以用xpath直接提取所需的元素的值。比如想要获取上面test.xml文件的x, y坐标:


tree = etree.parse("test.xml")
# get bbox
for bbox in tree.xpath('//bndbox'):  # 获取bndbox元素的内容
 for corner in bbox.getchildren(): # 便利bndbox元素下的子元素
   print corner.text  # string类型

参考

http://lxml.de/tutorial.html

https://stackoverflow.com/questions/12657043/parse-xml-with-lxml-extract-element-value

来源:http://www.cnblogs.com/arkenstone/p/7338978.html

标签:python,lxml,xml
0
投稿

猜你喜欢

  • 解决pytorch 的state_dict()拷贝问题

    2022-10-05 22:03:57
  • Webpack 实现 Node.js 代码热替换

    2024-05-13 10:04:14
  • WEB2.0网页制作标准教程(10)自适应高度

    2008-02-19 19:21:00
  • selenium2.0中常用的python函数汇总

    2023-04-05 13:15:11
  • python实现字符串加密 生成唯一固定长度字符串

    2022-02-04 21:02:27
  • ORACLE 常用函数总结(80个)第1/2页

    2009-09-18 13:23:00
  • Python使用内置函数setattr设置对象的属性值

    2021-10-09 20:32:44
  • opencv python图像梯度实例详解

    2021-05-17 23:26:30
  • Python多线程编程(二):启动线程的两种方法

    2023-11-27 16:15:48
  • ASP GetRef 函数指针试探

    2011-03-16 11:09:00
  • Python漏洞验证程序Poc利用入门到实战编写

    2022-05-08 00:00:31
  • 为什么MySQL不建议使用SELECT *

    2024-01-26 21:29:44
  • 解决python spyder 突然打不开的问题

    2022-07-07 19:43:06
  • Bootstrap4如何定制自己的颜色和风格

    2024-05-13 09:19:09
  • 定位后无法选择容器的内容解决方案

    2008-07-30 12:08:00
  • 使用python解析json字段的3种方式实例

    2021-12-24 04:16:27
  • vue 虚拟DOM的原理

    2023-07-02 17:03:18
  • Python入门_浅谈for循环、while循环

    2021-02-07 13:17:23
  • 关于大型页游后端管理系统的一点经验和个人见解

    2023-05-02 06:21:16
  • JavaScript中的Promise使用详解

    2024-04-18 10:53:52
  • asp之家 网络编程 m.aspxhome.com