python实现XML文件解析与修改

import pdb

import os
import xml.etree.ElementTree as ET

# python 3.2

def deil_file( dirName, fileName ):
    tree = ET.parse(dirName + fileName)
    root = tree.getroot()
    #print(root)

    #filename = root.find('path').text
    #filename = filename[:-4]
    #print (filename) 

    for object in root.findall('object'): #找到root节点下的所有object节点 
        object.find('name').text = "obj"  

    #for files in my_files:
    #    if "lab" in root.find('object').find('name').text:
    #        root.find('object').find('name').text = "obj" 
    #obj = root.find('object').find('name').text
    #obj = obj[:-4]
    #print (obj) 

    #print('\n')
    #root[0].text = "VOC2007"
    #root[1].text = root[1].text + ".jpg"
    tree.write('Annotations_2/' + fileName)
    #print(root[1].text)
    print('\n')
    #print(root[6].text)
    #pdb.set_trace()

my_files = os.listdir("/home/Desktop/workpy/Annotations/");
for files in my_files:
    if "xml" in files:
        # print(files)
        deil_file("/home/Desktop/workpy/Annotations/", files)
转载请注明: http://blog.csdn.net/forest_world

你可能感兴趣的:(Deep,Learning,Python)