csv_update.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import pandas as pd
  2. from functools import reduce
  3. # source_csv = 'data\\Kontenrahmen_Import.csv'
  4. # target_csv = 'data\\Kontenrahmen.csv'
  5. # result_csv = 'data\\Kontenrahmen_Ergebnis.csv'
  6. # source_csv = 'P:\\SKR51_GCStruct\\Kontenrahmen_Vorlage.csv'
  7. # target_csv = 'P:\\SKR51_GCStruct\\GCStruct_Portal\\Kontenrahmen\\Kontenrahmen.csv'
  8. # result_csv = 'P:\\SKR51_GCStruct\\GCStruct_Portal\\Kontenrahmen\\Kontenrahmen_neu.csv'
  9. base_dir = 'V:\\Kunden\\Luchtenberg\\1 Umstellung SKR51\\GCStruct_Modell\\Kontenrahmen\\'
  10. source_csv = base_dir + 'Kontenrahmen_Vorlage.csv'
  11. target_csv = base_dir + 'Kontenrahmen.csv'
  12. result_csv = base_dir + 'Kontenrahmen_neu.csv'
  13. debug = False
  14. cols_pkey = ['Konto_Nr']
  15. def update(d, other):
  16. d.update(dict(dict(other)))
  17. return d
  18. def get_dict(cols, type):
  19. return dict(dict(zip(cols, [type] * len(cols))))
  20. # cols_dict = reduce(update, (get_dict(cols_pkey, np.str), get_dict(cols_str, np.str), get_dict(cols_float, np.float)), {})
  21. df_source = pd.read_csv(source_csv, decimal=',', sep=';', encoding='latin-1', converters={i: str for i in range(0, 200)})
  22. df_source['pkey'] = reduce(lambda x, y: x + '_' + df_source[y], cols_pkey, '')
  23. df_source = df_source.set_index('pkey')
  24. df_target = pd.read_csv(target_csv, decimal=',', sep=';', encoding='latin-1', converters={i: str for i in range(0, 200)})
  25. target_columns = df_target.columns
  26. df_target['pkey'] = reduce(lambda x, y: x + '_' + df_target[y], cols_pkey, '')
  27. df_target = df_target.set_index('pkey')
  28. df_join = df_source.join(df_target, how='left', rsuffix='_other')[target_columns]
  29. df_remain = df_target.join(df_source, how='left', rsuffix='_other')
  30. df_remain = df_remain[pd.isna(df_remain[cols_pkey[0] + '_other'])][target_columns]
  31. # df_result = df_join[(df_join['Gesamt'] != 0) & (df_join['Serviceberater'] != "")]
  32. # veraltete Konten nicht löschen
  33. df_join = df_join.append(df_remain).sort_index()
  34. df_join.to_csv(result_csv, decimal=',', sep=';', encoding='latin-1', index=None)