erfa-adress-formatter/main.py
2024-09-03 20:36:11 +02:00

47 lines
1.5 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/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 <from_file> <to_file> [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)