import pandas as pd import os import json mapping_df = pd.read_excel(r'CMS-SCADA-关联.xlsx') mapping_df = mapping_df.dropna(subset=['id']) mapping_dict = dict() for index,row in mapping_df.iterrows(): mapping_dict[row['cms_id']] = row json_datas = json.load(open(r'wind_data_all.json','r')) exists_json = list() department_map = {'发电机': '发电机', '齿轮箱': '齿轮箱', '行星': '齿轮箱', '主轴': '主轴'} for key,row in mapping_dict.items(): if key in json_datas.keys(): json_data = json_datas[key] json_data['wind_farm_id'] = row['id'] json_data['wind_turbine_type'] = row['type'] json_data['wind_turbine_id'] = row['wind_turbine_id'] # childrens = json_data['children'] # js_childrens = list() # for children in childrens: # pointName = children['name'] # for k,v in department_map.items(): # if k in pointName: # children['department_name'] = v # js_childrens.append(children) # break # json_data['children'] = js_childrens del json_data['children'] exists_json.append(json_data) print(len(exists_json)) sl1500_items = [item for item in exists_json if item.get('wind_turbine_type') == 'SL1500-82'] my52_items = [item for item in exists_json if item.get('wind_turbine_type') == 'MY5.2se-166'] other_items = [item for item in exists_json if item.get('wind_turbine_type') not in ('SL1500-82', 'MY5.2se-166')] other_1 = [item for i, item in enumerate(other_items) if i % 2 == 0] other_2 = [item for i, item in enumerate(other_items) if i % 2 == 1] exists_json_1 = sl1500_items + other_1 exists_json_2 = my52_items + other_2 print(len(exists_json_1),len(exists_json_2),len(exists_json_1) + len(exists_json_2)) with open(r'wind_data_with_scada_1.json','w') as f: json.dump(exists_json_1,f,ensure_ascii=False,indent=4) with open(r'wind_data_with_scada_2.json','w') as f: json.dump(exists_json_2,f,ensure_ascii=False,indent=4)