use_data_generate_csv11.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import json
  2. import os
  3. def init_shunxu():
  4. with open('cedian.txt', 'r', encoding='utf8') as f:
  5. datas = [i.strip() for i in f.readlines() if i.strip()]
  6. result_map = dict()
  7. for data in datas:
  8. da = data.split('\t')
  9. point = Mesurepoint(da[0], da[2], da[4], da[5], da[6])
  10. if point.wind_factory in result_map.keys():
  11. if point.wind_no in result_map[point.wind_factory].keys():
  12. result_map[point.wind_factory][point.wind_no].append(point)
  13. else:
  14. result_map[point.wind_factory][point.wind_no] = [point]
  15. else:
  16. result_map[point.wind_factory] = {point.wind_no: [point]}
  17. return result_map
  18. class Mesurepoint(object):
  19. def __init__(self, wind_factory, wind_no, cn_name, shuxun, en_name):
  20. self.wind_factory = wind_factory
  21. self.wind_no = wind_no
  22. self.cn_name = cn_name
  23. self.shuxun = int(shuxun) + 16388
  24. self.en_name = en_name
  25. def mult_solver(wind_maps, seconds):
  26. for wind_factory in wind_maps.keys():
  27. for wind_no in wind_maps[wind_factory].keys():
  28. file_dir = os.path.join('104', wind_factory, str(seconds) + '秒')
  29. os.makedirs(file_dir, exist_ok=True)
  30. line_str = '\n'.join(wind_maps[wind_factory][wind_no])
  31. with open(os.path.join(file_dir, '风机' + str(wind_no) + '.csv'), 'w') as f:
  32. f.write(line_str)
  33. f.flush()
  34. def gene_data(wind_maps, base_data):
  35. wind_factories = result_map.keys()
  36. for wind_factory in wind_factories:
  37. if wind_factory not in wind_maps.keys():
  38. wind_maps[wind_factory] = {}
  39. for wind_no in result_map[wind_factory].keys():
  40. is_first = False
  41. if wind_no not in wind_maps[wind_factory].keys():
  42. wind_maps[wind_factory][wind_no] = list()
  43. is_first = True
  44. shunxuhaos = [point.shuxun for point in result_map[wind_factory][wind_no]]
  45. shunxuhaos.insert(0, '-1')
  46. if is_first:
  47. cols = ['time']
  48. for point in result_map[wind_factory][wind_no]:
  49. cols.append(point.cn_name)
  50. wind_maps[wind_factory][wind_no].append(','.join(cols))
  51. datas = [str(base_data[str(i)]) for i in shunxuhaos]
  52. wind_maps[wind_factory][wind_no].append(','.join(datas))
  53. def generate_data(base_data, all_datas, seconds):
  54. now_time = base_data['-1']
  55. wind_maps = dict()
  56. for data in all_datas:
  57. data['-1'] = data['-1'].split('.')[0]
  58. base_data.update(data)
  59. if now_time != data['-1'] and int(data['-1'][-2:]) % seconds == 0:
  60. now_time = data['-1']
  61. gene_data(wind_maps, base_data)
  62. mult_solver(wind_maps, seconds)
  63. if __name__ == '__main__':
  64. file_path = r'C:\Users\wzl\Desktop\241\2404 (复件)\20250310.txt'
  65. result_dict = dict()
  66. begin_end = [16388, 22744]
  67. base_data = {}
  68. for i in range(begin_end[0], begin_end[1] + 1):
  69. base_data[str(i)] = 0
  70. base_data['-1'] = ''
  71. result_map = init_shunxu()
  72. all_datas = list()
  73. with open(file_path, 'r') as f:
  74. line = f.readline()
  75. while line:
  76. fixed_str = line.replace('{', '{"').replace(':"', '":"').replace(',', ',"')
  77. line_data = {}
  78. line_data = json.loads(fixed_str)
  79. all_datas.append(line_data)
  80. line = f.readline()
  81. generate_data(base_data, all_datas, 1)
  82. generate_data(base_data, all_datas, 3)
  83. generate_data(base_data, all_datas, 5)