欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

深度学习xml文件读写(VOCdevkit/VOC2007/Annotations )

程序员文章站 2022-06-10 11:46:23
import osimport xml.dom.minidomimport requestsAnnotationPath = '/home/huacan/VOCdevkit/VOC2007/Annotations/'myDir = os.listdir(AnnotationPath)for filePath in myDir: print() print(AnnotationPath+filePath) dom = xml.dom.minidom.parse(Annot....
import os
import xml.dom.minidom
import requests

AnnotationPath = '/home/huacan/VOCdevkit/VOC2007/Annotations/'
myDir = os.listdir(AnnotationPath)
for filePath in myDir:
    print()
    print(AnnotationPath+filePath)
    dom = xml.dom.minidom.parse(AnnotationPath+filePath)
    rootdata = dom.documentElement
    #print(rootdata.toxml())
    try:
        filename = rootdata.getElementsByTagName('filename')[0].childNodes[0].data
        folder = rootdata.getElementsByTagName('folder')[0].childNodes[0].data
        print(folder)
        newName = folder.split('_')[1]

        rootdata.getElementsByTagName('name')[0].childNodes[0].data = newName
        rootdata.getElementsByTagName('filename')[0].childNodes[0].data = newName+filename.lstrip('cameraCapture')

        with open(AnnotationPath+filePath, 'w', encoding='utf-8') as f:  
            dom.writexml(f, addindent='\t', newl='\n',encoding='utf-8')

        dom = xml.dom.minidom.parse(AnnotationPath+filePath)
        rootdata = dom.documentElement

        nameN = rootdata.getElementsByTagName('name')[0].childNodes[0].data
        filenameN = rootdata.getElementsByTagName('filename')[0].childNodes[0].data
        print(nameN,filenameN)

    except:
        pass

 

本文地址:https://blog.csdn.net/zheng_ruiguo/article/details/107640605