python利用lxml讀寫(xiě)xml格式的文件
之前在轉(zhuǎn)換數(shù)據(jù)集格式的時(shí)候需要將json轉(zhuǎn)換到xml文件,用lxml包進(jìn)行操作非常方便。
1. 寫(xiě)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文件內(nèi)容如下:
```
如果需要在anno_tree的基礎(chǔ)上加其他標(biāo)簽的話用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坐標(biāo):
tree = etree.parse("test.xml") # get bbox for bbox in tree.xpath('//bndbox'): # 獲取bndbox元素的內(nèi)容 for corner in bbox.getchildren(): # 便利bndbox元素下的子元素 print corner.text # string類(lèi)型
參考
https://stackoverflow.com/questions/12657043/parse-xml-with-lxml-extract-element-value
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于django 的orm中非主鍵自增的實(shí)現(xiàn)方式
這篇文章主要介紹了基于django 的orm中非主鍵自增的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05利用Python命令行傳遞實(shí)例化對(duì)象的方法
最近在工作中遇到了一個(gè)問(wèn)題,需要接收啟動(dòng)腳本傳遞過(guò)來(lái)的實(shí)例化后的對(duì)象,通過(guò)在網(wǎng)上查找資料發(fā)現(xiàn)了兩個(gè)方法,文中通過(guò)實(shí)例代碼就給大家詳細(xì)介紹了這兩種方法,有需要的朋友們可以參考借鑒,下面來(lái)一起看看吧。2016-11-11超詳細(xì)注釋之OpenCV Haar級(jí)聯(lián)檢測(cè)器進(jìn)行面部檢測(cè)
這篇文章主要介紹了OpenCV Haar級(jí)聯(lián)檢測(cè)器進(jìn)行面部檢測(cè),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09使用python實(shí)現(xiàn)將視頻中的音頻分離出來(lái)
這篇文章主要介紹了使用python實(shí)現(xiàn)將視頻中的音頻分離出來(lái),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11R vs. Python 數(shù)據(jù)分析中誰(shuí)與爭(zhēng)鋒?
R和Python兩者誰(shuí)更適合數(shù)據(jù)分析領(lǐng)域?在某些特定情況下誰(shuí)會(huì)更有優(yōu)勢(shì)?還是一個(gè)天生在各方面都比另一個(gè)更好?2017-10-10Python實(shí)現(xiàn)switch/case語(yǔ)句
與Java、C\C++等語(yǔ)言不同,Python中是不提供switch/case語(yǔ)句的,這一點(diǎn)讓我感覺(jué)到很奇怪。我們可以通過(guò)如下幾種方法來(lái)實(shí)現(xiàn)switch/case語(yǔ)句2021-08-08使用python將微信image下.dat文件解密為.png的方法
這篇文章主要介紹了使用python將微信image下.dat文件解密為.png的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11Python調(diào)用Prometheus監(jiān)控?cái)?shù)據(jù)并計(jì)算
Prometheus是一套開(kāi)源監(jiān)控系統(tǒng)和告警為一體,由go語(yǔ)言(golang)開(kāi)發(fā),是監(jiān)控+報(bào)警+時(shí)間序列數(shù)據(jù)庫(kù)的組合。本文將介紹Python如何調(diào)用Prometheus實(shí)現(xiàn)數(shù)據(jù)的監(jiān)控與計(jì)算,需要的可以參考一下2021-12-12