对比文件夹列名差值.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. from utils.file.trans_methods import *
  2. def boolean_is_check_data(df_cols):
  3. fault_list = ['快速停机', '故障名称', '故障代码', '故障停机', '人工停机', '风机紧急停机', '工作模式']
  4. df_cols = [str(i).split('_')[-1] for i in df_cols]
  5. for fault in fault_list:
  6. if fault in df_cols:
  7. return True
  8. return False
  9. def compareTwoFolders(list1, other_dfs):
  10. for is_falut in [True]:
  11. result_df = pd.DataFrame()
  12. # for df1 in df1s:
  13. # tmp_list = [str(i).split('_')[-1] for i in list(df1.columns) if i != 'sheet_name']
  14. # if is_falut:
  15. # if boolean_is_check_data(df1.columns):
  16. # list1.extend(tmp_list)
  17. # else:
  18. # if not boolean_is_check_data(df1.columns):
  19. # list1.extend(tmp_list)
  20. set1 = set(list1)
  21. list1 = list(set1)
  22. list1.sort()
  23. result_df['完整列名'] = list1
  24. for wind_name, dfs in other_dfs.items():
  25. list2 = list()
  26. for df in dfs:
  27. tmp_list = [str(i).split('_')[-1] for i in list(df.columns) if i != 'sheet_name']
  28. if is_falut:
  29. if boolean_is_check_data(df.columns):
  30. list2.extend(tmp_list)
  31. else:
  32. if not boolean_is_check_data(df.columns):
  33. list2.extend(tmp_list)
  34. set2 = set(list2)
  35. list2 = list(set2)
  36. list2.sort()
  37. list3 = list(set1 - set2)
  38. list3.sort()
  39. # list4 = list(set2 - set1)
  40. # list4.sort()
  41. # print(list3)
  42. # print(list4)
  43. max_count = len(list1)
  44. list1.extend([''] * (max_count - len(list1)))
  45. list2.extend([''] * (max_count - len(list2)))
  46. list3.extend([''] * (max_count - len(list3)))
  47. # list4.extend([''] * (max_count - len(list4)))
  48. result_df[str(wind_name) + '字段'] = list2
  49. result_df[str(wind_name) + '比完整列名少字段'] = list3
  50. # result_df['风机' + str(wind_name) + '_比风机1多字段'] = list4
  51. file_name = 'col_compare.csv' if not is_falut else 'col_compare_fault.csv'
  52. result_df.to_csv(file_name, encoding='utf-8', index=False)
  53. if __name__ == '__main__':
  54. begin = datetime.datetime.now()
  55. dir2 = r'D:\data\新华水电\风机SCADA数据'
  56. files2 = read_excel_files(dir2)
  57. other_dfs = dict()
  58. list1 = list()
  59. for file in files2:
  60. month = path.basename(path.dirname(path.dirname(file)))[0:2]
  61. wind_name = month + path.basename(path.dirname(file)).split('#')[0] + '号风机'
  62. df = read_file_to_df(file, nrows=1)
  63. if boolean_is_check_data(df.columns):
  64. list1.extend([str(i).split('_')[-1] for i in list(df.columns) if i != 'sheet_name'])
  65. if wind_name in other_dfs.keys():
  66. other_dfs[wind_name].append(df)
  67. else:
  68. other_dfs[wind_name] = [df]
  69. # with multiprocessing.Pool(10) as pool:
  70. # df2s = pool.starmap(read_file_to_df, [(file, list(), None, 1) for file in files2])
  71. #
  72. list1 = [i for i in list(set(list1)) if i != 'sheet_name']
  73. compareTwoFolders(list1, other_dfs)
  74. print(datetime.datetime.now() - begin)