import os, sys, time, shutil, subprocess, shlex, json import xml.etree.ElementTree as ET import platform if platform.system() == 'Windows': sys.path.append('e:\\libs\\') #sys.path.append('libs') else: sys.path.append('/data/deploy/make3d/make2/libs/') import config, libs, libs_db,common if __name__ == '__main__': # 解析XML文件 pid = "112322" file_path = os.path.join(config.workdir, pid, f'{pid}.rcproj') tree = ET.parse(file_path) root = tree.getroot() # 遍历所有的reconstructions节点 for reconstruction in root.findall('reconstructions'): for component in reconstruction.findall('component'): if component.find('model') == None: reconstruction.remove(component) continue # 获取所有包含model标签的component节点 components_with_model = [component for component in reconstruction.findall('component') if component.find('model') is not None] print(components_with_model) # 如果包含model标签的component节点数量大于1,则按照model数量降序排序 if len(components_with_model) > 1: components_with_model.sort(key=lambda x: len(x.findall('model')), reverse=False) for i in range(len(components_with_model)-1): reconstruction.remove(components_with_model[i]) # 保存修改后的XML文件 tree.write(file_path)