123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- import csv
- from pathlib import Path
- from misc.file_backup import copy_file_with_timestamp
- def count_unique_emails(csv_file):
- emails = set()
- with open(csv_file, "r") as file:
- reader = csv.reader(file, delimiter=";")
- for row in reader:
- if row[2] in ["Empfaenger", ""]:
- continue
- emails.add(row[2])
- with open(str(Path(csv_file).parent) + "\\email_empfaenger.csv", "w") as fwh:
- for email in sorted(emails):
- fwh.write(email + "\n")
- return len(emails)
- def convert_csv_in_folder(folder, export_csv, all_reports=False):
- row_count = 0
- with open(export_csv, "w") as fwh:
- fwh.write("Versand;Datei;Empfaenger\n")
- for file in Path(folder).glob("*.csv"):
- with file.open("r") as frh:
- reader = csv.reader(frh, delimiter=";")
- for row in reader:
- if len(row) < 2 or row[1] in ["Empfaenger"]:
- continue
- if row[1] == "":
- if all_reports:
- fwh.write(file.name + ";" + row[0] + ";\n")
- row_count += 1
- continue
- email_list = row[1].lower().split(",")
- for email in sorted(email_list):
- fwh.write(file.name + ";" + row[0] + ";" + email.strip() + "\n")
- row_count += 1
- return row_count
- def convert_combined_to_folder(csv_file, export_folder):
- result = {}
- with open(csv_file, "r") as frh:
- reader = csv.reader(frh, delimiter=";")
- for row in reader:
- if row[0] in ["Versand"]:
- # Kopfzeile ignorieren
- continue
- if row[0] not in result:
- result[row[0]] = {}
- if row[1] not in result[row[0]]:
- result[row[0]][row[1]] = []
- result[row[0]][row[1]].append(row[2])
- # if row[0] != current_file:
- # if fwh is not None:
- # fwh.close()
- # current_file = row[0]
- # fwh = open(f"{export_folder}\\{current_file}", "w")
- # fwh.write(row[1] + ";" + row[2] + "\n")
- # fwh.close()
- # json.dump(result, open("export2\\test.json", "w"), indent=2)
- for file, reports in result.items():
- print(file)
- with open(f"{export_folder}\\{file}", "w") as fwh:
- fwh.write("Datei;Empfaenger\n")
- for report, mails in reports.items():
- fwh.write(report + ";" + ",".join(sorted(mails)) + "\n")
- def mail_csv_backup(base_dir: str) -> None:
- for f in Path(base_dir).glob("*.csv"):
- # print("* " + f.name)
- copy_file_with_timestamp(str(f), str(f.parent) + "\\backup")
- def convert_mail_csv(csv_file):
- pass
- def convert_folder_to_combined_csv(path: str):
- print("Sicherung der Versand-Intervall-Dateien")
- mail_csv_backup(path)
- export_csv = path + "\\info\\Versand_kombiniert.csv"
- row_count = convert_csv_in_folder(path, export_csv)
- print(f"Anzahl der Datei-Empfaenger-Kombinationen: {row_count}")
- row_count = count_unique_emails(export_csv)
- print(f"Anzahl der eindeutigen E-Mail-Adressen: {row_count}")
- export_csv2 = path + "\\info\\Versand_kombiniert_komplett.csv"
- convert_csv_in_folder(path, export_csv2, True)
- def workflow2():
- convert_combined_to_folder("info\\Versand_kombiniert.csv", "export2")
- if __name__ == "__main__":
- convert_folder_to_combined_csv()
- workflow2()
|