import os
import xml.etree.ElementTree as ET
def count_object_names(folder_path):
# 存放不同name的集合
names_set = set()
# 遍历文件夹中的所有文件
for filename in os.listdir(folder_path):
if filename.endswith('.xml'): # 确保处理的是XML文件
tree = ET.parse(os.path.join(folder_path, filename))
root = tree.getroot()
# 遍历所有的object元素
for obj in root.findall('object'):
# 获取name元素的文本内容并添加到集合中
name = obj.find('name').text
names_set.add(name)
return names_set
# 调用函数并传入文件夹路径
folder_path = r'D:\Desktop\xml' # 替换为你的文件夹路径
unique_names = count_object_names(folder_path)
# 打印结果
print(f"总共有 {len(unique_names)} 种不同的object name。")
for name in unique_names:
print(name)
标签:xml,name,object,查询,names,path,几类,folder
From: https://blog.csdn.net/Guo9898/article/details/142527639