统计XML标签种类和个数

# -*- coding: utf-8 -*-

"""
Created on 20-3-6

@author: zmcldw

@requirements: PyCharm 2018.2.4; Python 3.6.0 |Anaconda 4.3.1 (64-bit)

@decription: statistic the numbers of classes in labels
"""


import os
import xml.dom.minidom


AnnoPath = ''
Annolist = os.listdir(AnnoPath)
rate = {} # 创建一个字典用于存放标签名和对应的出现次数
# total = 0
for annotation in Annolist:
    fullname = AnnoPath + annotation
    dom = xml.dom.minidom.parse(fullname) # 打开XML文件
    collection = dom.documentElement # 获取元素对象
    objectlist = collection.getElementsByTagName('object') # 获取标签名为object的信息
    for object in objectlist:
        namelist = object.getElementsByTagName('name') # 获取子标签name的信息
        objectname = namelist[0].childNodes[0].data # 取到name具体的值
        if objectname not in rate: # 判断字典里有没有标签,如无添加相应字段
            rate[objectname] = 0
        rate[objectname] += 1
        # total += 1

print(rate)
# print(total)

# 画图
# import matplotlib.pyplot as plt
# object = []
# number = []
# for key in rate:
#     object.append(key)
#     number.append(rate[key])
# plt.figure()
# plt.bar(object, number)
# plt.title('result')
# plt.show()
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值