csv_update.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 = (
  10. "V:\\Kunden\\Luchtenberg\\1 Umstellung SKR51\\GCStruct_Modell\\Kontenrahmen\\"
  11. )
  12. source_csv = base_dir + "Kontenrahmen_Vorlage.csv"
  13. target_csv = base_dir + "Kontenrahmen.csv"
  14. result_csv = base_dir + "Kontenrahmen_neu.csv"
  15. debug = False
  16. cols_pkey = ["Konto_Nr"]
  17. def update(d, other):
  18. d.update(dict(dict(other)))
  19. return d
  20. def get_dict(cols, type):
  21. return dict(dict(zip(cols, [type] * len(cols))))
  22. # cols_dict = reduce(update, (get_dict(cols_pkey, np.str), get_dict(cols_str, np.str), get_dict(cols_float, np.float)), {})
  23. df_source = pd.read_csv(
  24. source_csv,
  25. decimal=",",
  26. sep=";",
  27. encoding="latin-1",
  28. converters={i: str for i in range(0, 200)},
  29. )
  30. df_source["pkey"] = reduce(lambda x, y: x + "_" + df_source[y], cols_pkey, "")
  31. df_source = df_source.set_index("pkey")
  32. df_target = pd.read_csv(
  33. target_csv,
  34. decimal=",",
  35. sep=";",
  36. encoding="latin-1",
  37. converters={i: str for i in range(0, 200)},
  38. )
  39. target_columns = df_target.columns
  40. df_target["pkey"] = reduce(lambda x, y: x + "_" + df_target[y], cols_pkey, "")
  41. df_target = df_target.set_index("pkey")
  42. df_join = df_source.join(df_target, how="left", rsuffix="_other")[target_columns]
  43. df_remain = df_target.join(df_source, how="left", rsuffix="_other")
  44. df_remain = df_remain[pd.isna(df_remain[cols_pkey[0] + "_other"])][target_columns]
  45. # df_result = df_join[(df_join['Gesamt'] != 0) & (df_join['Serviceberater'] != "")]
  46. # veraltete Konten nicht löschen
  47. df_join = df_join.append(df_remain).sort_index()
  48. df_join.to_csv(result_csv, decimal=",", sep=";", encoding="latin-1", index=None)