fengxiang_fengdianchang.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. from multiprocessing import Pool
  2. import sys, os
  3. path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  4. print(path)
  5. sys.path.insert(0, path)
  6. print(sys.path)
  7. from utils.file.trans_methods import *
  8. from utils.systeminfo.sysinfo import use_files_get_max_cpu_count
  9. def read_and_save_file(filename):
  10. try:
  11. basename = os.path.basename(filename)
  12. wind_number = basename.split("_")[0]
  13. df = read_file_to_df(filename, header=1)
  14. df['风机号'] = wind_number
  15. df['描述'] = pd.to_datetime(df['描述'], format='%d-%m-%Y %H:%M:%S')
  16. df.set_index(keys=['描述', '风机号'], inplace=True)
  17. return wind_number, df
  18. except Exception as e:
  19. print(basename, 'error')
  20. raise e
  21. if __name__ == '__main__':
  22. read_path = r'/data/download/collection_data/1进行中/枫香风电场-贵州-大唐/收资数据/枫香风电场收资表/1.10分钟SCADA数据'
  23. save_path = r'/data/download/collection_data/1进行中/枫香风电场-贵州-大唐/清理数据/枫香风电场收资表/1.10分钟SCADA数据'
  24. # read_path = r'D:\trans_data\枫香\收资数据\min'
  25. # save_path = r'D:\trans_data\枫香\清理数据\min'
  26. create_file_path(save_path, False)
  27. all_fils = read_excel_files(read_path)
  28. process_count = use_files_get_max_cpu_count(all_fils)
  29. with Pool(process_count) as pool:
  30. results = pool.starmap(read_and_save_file, [(i,) for i in all_fils])
  31. df_dict = dict()
  32. for result in results:
  33. wind_number, df = result
  34. cols = list(df.columns)
  35. cols.sort()
  36. cols_str = '-'.join(cols)
  37. if wind_number in df_dict.keys():
  38. if cols_str in df_dict[wind_number].keys():
  39. df_dict[wind_number][cols_str] = pd.concat([df_dict[wind_number][cols_str], df], axis=0)
  40. else:
  41. df_dict[wind_number][cols_str] = df
  42. else:
  43. df_dict[wind_number] = {cols_str: df}
  44. for wind_number, cols_dict in df_dict.items():
  45. df = pd.concat(cols_dict.values(), axis=1)
  46. df.sort_index(inplace=True)
  47. df.reset_index(inplace=True)
  48. df.to_csv(os.path.join(save_path, f"{wind_number}.csv"), encoding="utf-8", index=False)