import json import os def init_shunxu(): with open('cedian.txt', 'r', encoding='utf8') as f: datas = [i.strip() for i in f.readlines() if i.strip()] result_map = dict() for data in datas: da = data.split('\t') point = Mesurepoint(da[0], da[2], da[4], da[5], da[6]) if point.wind_factory in result_map.keys(): if point.wind_no in result_map[point.wind_factory].keys(): result_map[point.wind_factory][point.wind_no].append(point) else: result_map[point.wind_factory][point.wind_no] = [point] else: result_map[point.wind_factory] = {point.wind_no: [point]} return result_map class Mesurepoint(object): def __init__(self, wind_factory, wind_no, cn_name, shuxun, en_name): self.wind_factory = wind_factory self.wind_no = wind_no self.cn_name = cn_name self.shuxun = int(shuxun) + 16388 self.en_name = en_name def mult_solver(wind_maps): for wind_factory in wind_maps.keys(): for wind_no in wind_maps[wind_factory].keys(): file_dir = os.path.join('104_origin', wind_factory) os.makedirs(file_dir, exist_ok=True) line_str = '\n'.join(wind_maps[wind_factory][wind_no]) with open(os.path.join(file_dir, '风机' + str(wind_no) + '.csv'), 'w') as f: f.write(line_str) f.flush() def gene_data(wind_maps, data): wind_factories = result_map.keys() for wind_factory in wind_factories: if wind_factory not in wind_maps.keys(): wind_maps[wind_factory] = {} for wind_no in result_map[wind_factory].keys(): is_first = False if wind_no not in wind_maps[wind_factory].keys(): wind_maps[wind_factory][wind_no] = list() is_first = True shunxuhaos = [point.shuxun for point in result_map[wind_factory][wind_no]] shunxuhaos.insert(0, '-1') if is_first: cols = ['time'] for point in result_map[wind_factory][wind_no]: cols.append(point.cn_name) wind_maps[wind_factory][wind_no].append(','.join(cols)) datas = [str(data[str(i)] if str(i) in data.keys() else "") for i in shunxuhaos] if len([i for i in datas if i != ""]) > 1: wind_maps[wind_factory][wind_no].append(','.join(datas)) def generate_data(all_datas): wind_maps = dict() for data in all_datas: gene_data(wind_maps, data) mult_solver(wind_maps) if __name__ == '__main__': # file_path = r'C:\Users\wzl\Desktop\241\2404 (复件)\20250310.txt' file_path = '原始数据.txt' result_dict = dict() begin_end = [16388, 22744] result_map = init_shunxu() all_datas = list() with open(file_path, 'r') as f: line = f.readline() while line: fixed_str = line.replace('{', '{"').replace(':"', '":"').replace(',', ',"') line_data = {} line_data = json.loads(fixed_str) all_datas.append(line_data) line = f.readline() generate_data(all_datas)