python建立xml python建立xml

2021-10-12 11:51:16 字數 2708 閱讀 9166

參考了中的實現了一遍

# -*- coding: utf-8 -*-

created on fri apr 01 15:37:46 2016

@author: raoqiang

#coding=utf-8

import xml.dom.minidom as minidom

from xml.dom.minidom import document

doc = document() #建立dom文件物件

def create_annotation(doc,file)

annot = doc.createelement('annotation') #建立根元素

folder = doc.createelement('folder')

folder_text = doc.createtextnode('myfolderpath') #元素內容寫入

fname = doc.createelement('filename')

fname_text = doc.createtextnode('0001.png') #元素內容寫入

src = doc.createelement('source')

db = doc.createelement('database')

db_text = doc.createtextnode('the synapse database');

annt = doc.createelement('annotation')

annt_text = doc.createtextnode('the synapse database');

img= doc.createelement('image')

img_text = doc.createtextnode('synapseem')

flickrid = doc.createelement('flickrid')

flickrid_text = doc.createtextnode(str(0))

owner = doc.createelement('owner')

flickrid = doc.createelement('flickrid')

flickrid_text = doc.createtextnode('i do not know')

size = doc.createelement('size')

width = doc.createelement('width')

width_text = doc.createtextnode(str(239))

height = doc.createelement('height')

height_text = doc.createtextnode(str(239))

depth = doc.createelement('depth')

depth_text = doc.createtextnode(str(1))

seg = doc.createelement('segmented')

seg_text = doc.createtextnode(str(0))

obj = doc.createelement('object')

name = doc.createelement('name')

name_text = doc.createtextnode('synapse');

pos = doc.createelement('pose')

pos_text = doc.createtextnode('unspecified')

trun = doc.createelement('truncated')

trun_text = doc.createtextnode(str(0))

diffi = doc.createelement('difficult')

diffi_text = doc.createtextnode(str(0))

bndbox = doc.createelement('bndbox')

xmin = doc.createelement('xmin')

xmin_text = doc.createtextnode(str(157))

xmax = doc.createelement('xmax')

xmax_text = doc.createtextnode(str(157))

ymin = doc.createelement('ymin')

ymin_text = doc.createtextnode(str(157))

ymax = doc.createelement('ymax')

ymax_text = doc.createtextnode(str(157))

########### 將dom物件doc寫入檔案

f = open('d:\\labproj\\faster_rcnn\\annotation.xml','w')

f.write(doc.toprettyxml(indent = ''))

f.close()

生成了如下的xml:基本照這個例子就可以學會怎麼建立節點和生成資料,關於其中變數的操作還有別的函式

myfolderpath

0001.png

the synapse database

synapseem

i do not know

synapse

unspecified

python建立子類 python 建立子類

如果你的類沒有從任何祖先類派生,可以使用object作為父類的名字。經典類的宣告唯一不同之處在於其 沒有從祖先類派生 此時,沒有圓括號 usr bin env python coding utf 8 class classicclasswithoutsuperclasses def fun1 sel...

python建立目錄函式 python 建立目錄

python對檔案的操作還算是方便的,只需要包含os模組進來,使用相關函式即可實現目錄的建立。主要涉及到三個函式 1 os.path.exists path 判斷乙個目錄是否存在 2 os.makedirs path 多層建立目錄 3 os.mkdir path 建立目錄 demo 直接上 def ...

python建立新檔案 python建立和刪除檔案

usr bin python coding utf 8 指定編碼格式,python預設unicode編碼 import os directory dir os.chdir directory 切換到directory目錄 cwd os.getcwd 獲取當前目錄即dir目錄下 print curre...