解决xml文件中label下数据路径统一的问题

感觉这个题目写得有点大了,其实就是label(.xml)文件中path节点不统一的问题。其实问题很容易解决,直接修改xml文件path节点的内容即可

简单的实现了1000以内的label

#Create by FYZ 2018-04-26 NEU

from xml.etree.ElementTree import ElementTree, Element

def read_xml(in_path):
    tree = ElementTree()
    tree.parse(in_path)
    return tree
def write_xml(tree, out_path):

  tree.write(out_path, encoding="utf-8",xml_declaration=True)

if __name__ == "__main__":
    path = "G://get_img//ChinaVehicle2018//Annotations//"
    for a in range(0, 612):
        if a >= 0 and a < 10:
            str_0 = str(a)
            a1_list = list(str_0)
            a1_list.insert(0, '0000')
            str_01 = "".join(a1_list)
            path_1 = path + str_01 + ".xml"
            tree = read_xml(path_1)
            nodes = tree.find('./path')
            str_r = "G:\get_img\ChinaVehicle2018\JPEGImages\\" + str_01 + ".jpg"
            nodes.text = str_r
            str_o = "G://my_python_pro//ann//" + str_01 + ".xml"
            write_xml(tree, str_o)

        elif a >= 10 and a < 100:
            str_1 = str(a)
            a2_list = list(str_1)
            a2_list.insert(0, '000')
            str_02 = "".join(a2_list)
            path_2 = path + str_02 + ".xml"
            tree = read_xml(path_2)
            nodes = tree.find('./path')
            str_r1 = "G:\get_img\ChinaVehicle2018\JPEGImages\\" + str_02 + ".jpg"
            nodes.text = str_r1
            str_o1 = "G://my_python_pro//ann//" + str_02 + ".xml"
            write_xml(tree, str_o1)

        else:
            str_4 = str(a)
            a4_list = list(str_4)
            a4_list.insert(0, '00')
            str_04 = "".join(a4_list)
            path_3 = path + str_04 + ".xml"
            tree = read_xml(path_3)
            nodes = tree.find('./path')
            str_r2 = "G:\get_img\ChinaVehicle2018\JPEGImages\\" + str_04 + ".jpg"
            nodes.text = str_r2
            str_o2 = "G://my_python_pro//ann//" + str_04 + ".xml"
            write_xml(tree, str_o2)

================================================================================================

方法2

像是标红的地方都可以改

# by FYJ

import xml.dom.minidom
import os

path='F://changeftpro//Changefathertreeproperties//d//'
sv_path='F://changeftpro//Changefathertreeproperties//e//'
files=os.listdir(path)
for xmlFile in files:
    dom=xml.dom.minidom.parse(os.path.join(path,xmlFile))
    root=dom.documentElement
    item=root.getElementsByTagName('name')
    for i in item:
        if(i.firstChild.data=='Pin_1st_0x01'):
            i.firstChild.data='Pin'
        if(i.firstChild.data=='Pin_1st_0x02'):
            i.firstChild.data='Pin'
        if(i.firstChild.data=='Pin_1st_0x03'):
            i.firstChild.data='Pin'
        if(i.firstChild.data=='Pin_1st_0x0F'):
            i.firstChild.data='Pin'
        if(i.firstChild.data=='Pin_2nd_0x01'):
            i.firstChild.data='Pin'
        if(i.firstChild.data=='Pin_2nd_0x02'):
            i.firstChild.data='Pin'
        if(i.firstChild.data=='Pin_2nd_0x03'):
            i.firstChild.data='Pin'
        if(i.firstChild.data=='Pin_2nd_0x0F'):
            i.firstChild.data='Pin'
        if(i.firstChild.data=='Insulator_1st_0x01'):
            i.firstChild.data='Insulator'
        if(i.firstChild.data=='Insulator_1st_0x02'):
            i.firstChild.data='Insulator'
        if(i.firstChild.data=='Insulator_1st_0x03'):
            i.firstChild.data='Insulator'
        if(i.firstChild.data=='Insulator_1st_0x0F'):
            i.firstChild.data='Insulator'
        if(i.firstChild.data=='Insulator_2nd_0x01'):
            i.firstChild.data='Insulator'
        if(i.firstChild.data=='Insulator_2nd_0x02'):
            i.firstChild.data='Insulator'
        if(i.firstChild.data=='Insulator_2nd_0x0F'):
            i.firstChild.data='Insulator'
        if(i.firstChild.data=='StrandedWire_0x01'):
            i.firstChild.data='StrandedWire'
        if(i.firstChild.data=='StrandedWire_0x02'):
            i.firstChild.data='StrandedWire'
        if(i.firstChild.data=='StrandedWire_0x0F'):
            i.firstChild.data='StrandedWire'
    with open(os.path.join(sv_path,xmlFile),'w') as fh:
        dom.writexml(fh)

 加上if语句,类名都可以改

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值