47 lines
1.5 KiB
Python
47 lines
1.5 KiB
Python
#!/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)
|