python读xml文件

图片标注时忘记有多少类别了,写了个程序,把所有图片的类别打印出来

代码如下:

s = []
for p in range(1250):
    p = p + 1
    if (p > 0 and p < 10): a = '0000' + str(p)
    elif (p >= 10 and p < 100): a = '000' + str(p)
    elif (p >= 100 and p < 1000): a = '00' + str(p)
    else: a = '0' + str(p)
    dom = xml.dom.minidom.parse('C:\\Users\\Administrator\\Desktop\\CarModelYHQ\\CarModelYHQ\\Annotations\\' + a + '.xml')

    root = dom.documentElement

    name=dom.getElementsByTagName('name')

    for i in range(len(name)):
        t = name[i].firstChild.data
        #print (t)
        x = len(s)
        flag = 0
        for j in range(x):
            if (t == s[j]): flag = 1
        if (flag == 0): 
            s.append(t)
            x = x+1
print (s)
print (x)

猜你喜欢

转载自blog.csdn.net/weixin_42755375/article/details/81669931