python 生成xml文件,以及美化的實(shí)例代碼
看代碼吧~
# -*- coding:utf-8 -*-
import os
import json
import numpy as np
#from xml.etree import ElementTree as etree
from xml.etree.ElementTree import Element
from xml.etree.ElementTree import SubElement
from xml.etree.ElementTree import ElementTree
imagePath = r'E:\Desktop\SteelCoilsDetection\test\images'
jsonPath = r'E:\Desktop\SteelCoilsDetection\test\json'
savePath = r'E:\Desktop\SteelCoilsDetection\test\xml'
jsonList = os.listdir(jsonPath)
for jsonName in jsonList:
print(jsonName)
readPath = os.path.join(jsonPath, jsonName)
# 打開json文件
with open(readPath, 'r') as file_loader:
jsonDic = json.load(file_loader)
# print(jsonDic.keys())
# dict_keys(['version', 'flags', 'shapes', 'imagePath', 'imageData', 'imageHeight', 'imageWidth'])
# 生成xml文件
annotation = Element('annotation')
folder = SubElement(annotation, 'folder')
folder.text = "images"
filename = SubElement(annotation, 'filename')
filename.text = jsonName.split('.')[0]
path = SubElement(annotation, 'path')
path.text = imagePath + jsonName.split('.')[0]
source = SubElement(annotation, 'source')
database = SubElement(source, 'database')
database.text = "Unknown"
size = SubElement(annotation, 'size')
width = SubElement(size, 'width')
width.text = str(jsonDic['imageWidth'])
height = SubElement(size, 'height')
height.text = str(jsonDic['imageHeight'])
depth = SubElement(size, 'depth')
depth.text = "3"
segmented = SubElement(annotation, 'segmented')
segmented.text = "0"
for shape in jsonDic['shapes']:
if shape["label"] == 'a':
continue
object = SubElement(annotation, 'object')
name = SubElement(object, 'name')
name.text = shape["label"]
pose = SubElement(object, 'pose')
pose.text = 'Unspecified'
truncated = SubElement(object, 'truncated')
truncated.text = str(0)
difficult = SubElement(object, 'difficult')
difficult.text = str(0)
points = shape['points']
mritx = np.array(points)
xxmin = min(mritx[:, 0])
xxmax = max(mritx[:, 0])
yymin = min(mritx[:, 1])
yymax = max(mritx[:, 1])
bndbox = SubElement(object, 'bndbox')
xmin = SubElement(bndbox, 'xmin')
xmin.text = str(int(xxmin))
ymin = SubElement(bndbox, 'ymin')
ymin.text = str(int(yymin))
xmax = SubElement(bndbox, 'xmax')
xmax.text = str(int(xxmax))
ymax = SubElement(bndbox, 'ymax')
ymax.text = str(int(yymax))
tree = ElementTree(annotation)
tree.write(os.path.join(savePath, jsonName.split('.')[0]+'.xml'), encoding = 'utf-8')
美化:
# -*- coding:utf-8 -*-
import os
from xml.etree import ElementTree # 導(dǎo)入ElementTree模塊
# elemnt為傳進(jìn)來的Elment類,參數(shù)indent用于縮進(jìn),newline用于換行
def prettyXml(element, indent, newline, level = 0):
# 判斷element是否有子元素
if element:
# 如果element的text沒有內(nèi)容
if element.text == None or element.text.isspace():
element.text = newline + indent * (level + 1)
else:
element.text = newline + indent * (level + 1) + element.text.strip() + newline + indent * (level + 1)
# 此處兩行如果把注釋去掉,Element的text也會(huì)另起一行
#else:
#element.text = newline + indent * (level + 1) + element.text.strip() + newline + indent * level
temp = list(element) # 將elemnt轉(zhuǎn)成list
for subelement in temp:
# 如果不是list的最后一個(gè)元素,說明下一個(gè)行是同級別元素的起始,縮進(jìn)應(yīng)一致
if temp.index(subelement) < (len(temp) - 1):
subelement.tail = newline + indent * (level + 1)
else: # 如果是list的最后一個(gè)元素, 說明下一行是母元素的結(jié)束,縮進(jìn)應(yīng)該少一個(gè)
subelement.tail = newline + indent * level
# 對子元素進(jìn)行遞歸操作
prettyXml(subelement, indent, newline, level = level + 1)
dir = r'E:\Desktop\SteelCoilsDetection\test\xml'
for fileName in os.listdir(dir):
print(fileName)
tree = ElementTree.parse(os.path.join(dir, fileName)) #解析test.xml這個(gè)文件,該文件內(nèi)容如上文
root = tree.getroot() #得到根元素,Element類
prettyXml(root, '\t', '\n') # 執(zhí)行美化方法
#ElementTree.dump(root) #顯示出美化后的XML內(nèi)容
tree.write(os.path.join(dir, fileName), encoding = 'utf-8')
補(bǔ)充:Python 標(biāo)準(zhǔn)庫 xml 詳解
對于簡單的 XML 解析處理, 可以使用標(biāo)準(zhǔn)庫 xml, 相對于第三方庫 lxml, xml 無需額外安裝, 但 xml 是用 Python 實(shí)現(xiàn)的, 性能不如 lxml
XML 的解析功能主要由 xml.etree.ElementTree 模塊完成, 其中包含兩個(gè)類, ElementTree 用于表示整個(gè) XML 文檔, 而 Element 表示文檔中的一個(gè)節(jié)點(diǎn)
示例數(shù)據(jù), 命名為 book.xml
<?xml version="1.0"?> <bookstore> <book name="西游記"> <author>吳承恩</author> <dynasty>明朝</dynasty> <similar name="封神演義" author="許仲琳"/> </book> <book name="紅樓夢"> <author>曹雪芹</author> <dynasty>清朝</dynasty> </book> <book name="三國演義"> <author>羅貫中</author> <dynasty>明末清初</dynasty> <similar name="三國志" author="陳壽"/> </book> </bookstore>
導(dǎo)入要解析的 XML 文檔, 并獲取文檔的根節(jié)點(diǎn)
import xml.etree.ElementTree as ET
tree = ET.parse("./book.xml")
root = tree.getroot()
也可以直接解析字符串
with open("./book.xml") as fp:
root = ET.fromstring(fp.read())
對于每一個(gè)節(jié)點(diǎn) Element:
通過列表接口可以訪問直接子節(jié)點(diǎn)
通過字典接口可以訪問屬性節(jié)點(diǎn), 也可通過 attrib 屬性(例如 root.attrib)得到真正的字典
其他還有 tag 屬性表示標(biāo)簽名, text 表示其包含的文本內(nèi)容
# 遍歷直接子節(jié)點(diǎn)
for book in root:
print(book.tag, book.attrib, book.get("name"))
# 訪問根節(jié)點(diǎn)下的第2個(gè)子節(jié)點(diǎn), 再向下訪問第1個(gè)子節(jié)點(diǎn)的文本, 也就是 "<author>曹雪芹</author>"
author = root[1][0].text
print(type(author), author)
打印輸出
book {'name': '西游記'} 西游記
book {'name': '紅樓夢'} 紅樓夢
book {'name': '三國演義'} 三國演義
<class 'str'> 曹雪芹
獲取到的文本結(jié)果與 lxml 不同, 這里的結(jié)果直接是字符串類型
遞歸函數(shù), 可以遍歷所有的后代節(jié)點(diǎn)
# 遞歸選擇所有標(biāo)簽名為 "similar" 的節(jié)點(diǎn)
for book in root.iter("similar"):
print(book.attrib)
打印輸出
{'name': '封神演義', 'author': '許仲琳'}
{'name': '三國志', 'author': '陳壽'}
XPath 語法
XPath 類似于文件路徑, 路徑中最末尾的部分表示要提取的內(nèi)容, 分隔符有兩種, "/"表示直接子節(jié)點(diǎn)的關(guān)系, "http://"表示所有的子節(jié)點(diǎn)
| 語法 | 含義 |
|---|---|
| tag | 匹配特定標(biāo)簽 |
| * | 匹配所有元素 |
| . | 當(dāng)前節(jié)點(diǎn), 用于相對路徑 |
| … | 父節(jié)點(diǎn) |
| [@attrib] | 匹配包含 attrib 屬性的節(jié)點(diǎn) |
| [@attrib=‘value'] | 匹配 attrib 屬性等于 value 的節(jié)點(diǎn) |
| [tag] | 匹配包含直接子節(jié)點(diǎn) tag 的節(jié)點(diǎn) |
| [tag=‘text'] | 匹配包含直接子節(jié)點(diǎn) tag 且子節(jié)點(diǎn)文本內(nèi)容為 text 的節(jié)點(diǎn) |
| [n] | 匹配第 n 個(gè)節(jié)點(diǎn) |
[] 前面必須有標(biāo)簽名, book[@name][similar] 匹配帶有 name 屬性以及 similar 直接子節(jié)點(diǎn)的 book 節(jié)點(diǎn), 然后將 book[@name][similar] 置于 XPath 路徑中, 例如 “/bookstore/book[@name][similar]”
可以通過 Element 對象的方法 findall(path) 和 find(path) 使用 XPath 語法, 次時(shí)路徑是從 Element 代表的節(jié)點(diǎn)開始, 也可以通過 ElementTree 對象調(diào)用 findall 與 find, 相當(dāng)于路徑從根節(jié)點(diǎn)開始
匹配到節(jié)點(diǎn), findall 返回所有匹配節(jié)點(diǎn)的列表, find 返回首個(gè)匹配節(jié)點(diǎn), 沒有匹配到節(jié)點(diǎn)時(shí), findall 返回空列表, find 返回 None
# . 表示 bookstore 節(jié)點(diǎn)
author_1 = tree.find("./book[@name='紅樓夢']/author").text
author_2 = tree.findtext("./book[@name='紅樓夢']/author")
print("紅樓夢作者:", author_1, author_2)
author_3 = root.find("./book/similar[@name='三國志']").get("author")
print("三國志作者:", author_3)
打印結(jié)果
紅樓夢作者: 曹雪芹 曹雪芹
三國志作者: 陳壽
findtext 類似于 find, 直接獲取節(jié)點(diǎn)的文本內(nèi)容
books_1 = root.findall("./book[similar]")
# 對于直接子節(jié)點(diǎn), 可以省略 ./
books_2 = root.findall("book[similar]")
print(books_1 == books_2)
for book in books_1:
print(book[0].text, book[1].text)
打印結(jié)果
True
吳承恩 明朝
羅貫中 明末清初
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
python人工智能human?learn繪圖創(chuàng)建機(jī)器學(xué)習(xí)模型
這篇文章主要為大家介紹了python人工智能human?learn繪圖就可以創(chuàng)建機(jī)器學(xué)習(xí)模型的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-11-11
如何將Python字符串轉(zhuǎn)換為JSON的實(shí)現(xiàn)方法
在本教程中,你將學(xué)習(xí)JSON的基礎(chǔ)知識,它是什么,常用在哪里以及它的語法,還將看到如何在Python中將字符串轉(zhuǎn)換為JSON,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12
Tensorflow 實(shí)現(xiàn)線性回歸模型的示例代碼
這篇文章主要介紹了Tensorflow 實(shí)現(xiàn)線性回歸模型,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
Python中xml.etree.ElementTree的使用示例
ElementTree是Python標(biāo)準(zhǔn)庫中的一個(gè)模塊,專門用于處理XML文件,它提供了解析、創(chuàng)建、修改和遍歷XML文檔的API,非常適合處理配置文件、數(shù)據(jù)交換格式和Web服務(wù)響應(yīng)等場景,本文就來介紹一下,感興趣的可以了解一下2024-09-09
python自動(dòng)化測試selenium執(zhí)行js腳本實(shí)現(xiàn)示例
這篇文章主要為大家介紹了python自動(dòng)化測試selenium執(zhí)行js腳本的實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11
Django之編輯時(shí)根據(jù)條件跳轉(zhuǎn)回原頁面的方法
今天小編就為大家分享一篇Django之編輯時(shí)根據(jù)條件跳轉(zhuǎn)回原頁面的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08
詳解Python 關(guān)聯(lián)規(guī)則分析
這篇文章主要介紹了Python 關(guān)聯(lián)規(guī)則分析的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-03-03

