批量修改voc資料集中xml屬性詳解

2021-09-05 12:37:14 字數 1426 閱讀 1107

import os

import os.path

import xml.dom.minidom

# c:\\users\chenlufei\desktop\\1234.xml

path="f:\\sss\\" #路徑(最好路徑下只有xml檔案,有其他檔案容易報錯)

files=os.listdir(path) #得到資料夾下所有檔名稱

s=for xmlfile in files: #遍歷資料夾

if not os.path.isdir(xmlfile): #判斷是否是資料夾,不是資料夾才開啟

print (xmlfile)

#todo

#xml檔案讀取操作

#將獲取的xml檔名送入到dom解析

dom=xml.dom.minidom.parse(os.path.join(path,xmlfile)) ###最核心的部分os.path.join(path,xmlfile),路徑拼接,輸入的是具體路徑

root=dom.documentelement

#獲取標籤對name/pose之間的值

name=root.getelementsbytagname('filename')

#重新命名class name

for i in range(len(name)):

print (name[i].firstchild.data)

name[i].firstchild.data='orange'

print( name[i].firstchild.data)

#儲存修改到xml檔案中

with open(os.path.join(path,xmlfile),'w') as fh:

dom.writexml(fh)

print('寫入name/pose ok!')

# with open('dom_write.xml','w',encoding='utf-8') as fh:

# # 4.writexml()第乙個引數是目標檔案物件,第二個引數是根節點的縮排格式,第三個引數是其他子節點的縮排格式,

# # 第四個引數制定了換行格式,第五個引數制定了xml內容的編碼。

# dom.writexml(fh,indent='',addindent='\t',newl='\n',encoding='utf-8')

# print('寫入xml ok!')

#xml檔案中第一行必須為<?xml version="1.0" ?>,且不能有空格否則報錯:

#xml.parsers.expat.expaterror: xml or text declaration not at start of entity: line 1, column 1

voc資料集批量修改標籤名字

在此附上 import io import sys import os import xml.etree.elementtree as et anno path修改為你存放xml檔案的位置,最後末尾需要加上 old annotation是修要修改的標籤名,new annotation是修改後的標籤名...

工具指令碼 VOC格式資料集修改XML檔案類別標籤

usr bin env python2 coding utf 8 import os import xml.etree.elementtree as et origin ann dir data 1 xml 設定原始標籤路徑為 annos new ann dir data 1 xml new 設定新...

利用python批量修改xml檔案

問題描述 對大量xml標籤進行修改floder的名字,以及path路徑。例如把1修改為train,把路徑修改為當前xml所在位置。xml格式如下 如下 usr bin python coding utf 8 import os import os.path from xml.etree.elemen...