initial commit
This commit is contained in:
commit
8c3a5568b7
2 changed files with 54 additions and 0 deletions
47
main.py
Normal file
47
main.py
Normal file
|
@ -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 <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)
|
7
readme.md
Normal file
7
readme.md
Normal file
|
@ -0,0 +1,7 @@
|
|||
# Convert CSV to DHL/POST CSV
|
||||
|
||||
```
|
||||
python main.py <input-file> <output-file> [sender-name]
|
||||
```
|
||||
|
||||
sender name defaults to 'Flipdot'
|
Loading…
Reference in a new issue