From 8c3a5568b73c5a3299738c2b80c1e22dd53aa1d7 Mon Sep 17 00:00:00 2001 From: KS_HTK Date: Tue, 3 Sep 2024 20:36:11 +0200 Subject: [PATCH] initial commit --- main.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ readme.md | 7 +++++++ 2 files changed, 54 insertions(+) create mode 100644 main.py create mode 100644 readme.md diff --git a/main.py b/main.py new file mode 100644 index 0000000..29ac263 --- /dev/null +++ b/main.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python + +import sys +import csv +import re + +def parse_string_to_int(s): + # Remove non-digit characters and convert to int + return int(re.sub(r'\D', '', s)) if re.sub(r'\D', '', s) else None + +keys = ['', 'Chaostreff-Postal-Name', 'Chaostreff-Postal-Remark', 'Chaostreff-Postal-Address', 'Chaostreff-Postal-Housenumber', 'Chaostreff-Postal-Postcode', 'Chaostreff-Postal-City', 'Chaostreff-Country'] +values = ['NAME', 'ZUSATZ', 'STRASSE', 'NUMMER', 'PLZ', 'STADT', 'LAND', 'ADRESS_TYP'] +data = [] + +if len(sys.argv) < 3: + print('Usage: main.py [sender]') + sys.exit(1) +from_file = sys.argv[1] +to_file = sys.argv[2] +if len(sys.argv) > 4: + sender = sys.argv[3] +else: + sender = 'Flipdot' + +with open(from_file, 'r', encoding='utf-8') as f: + reader = csv.reader(f) + header = next(reader) + + if header != keys: + print(f'Header mismatch: {header}') + sys.exit(1) + + for row in reader: + if row[0] == sender: + data = [row[1:4]+[row[4].replace('–', '-')]+row[5:]+['HOUSE']] + data + else: + add_type = 'POBOX' if row[3].startswith('Postfach') else 'HOUSE' + if add_type == 'POBOX': + data.append(row[1:3]+['']+[parse_string_to_int(row[3])]+row[5:]+['POBOX']) + else: + data.append(row[1:4]+[row[4].replace('–', '-')]+row[5:]+['HOUSE']) + +data = [values] + data + +with open(to_file, 'w', newline='') as f: + writer = csv.writer(f, delimiter=';') + writer.writerows(data) diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..90a2c9b --- /dev/null +++ b/readme.md @@ -0,0 +1,7 @@ +# Convert CSV to DHL/POST CSV + +``` +python main.py [sender-name] +``` + +sender name defaults to 'Flipdot' \ No newline at end of file