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

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