Created
December 6, 2020 16:16
-
-
Save hanslovsky/bc0903dd58cc367a59917d759124396a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import csv | |
mpix_header = ( | |
"First Name", | |
"Last Name", | |
"Family Name", | |
"Address Line 1", | |
"Address Line 2", | |
"City", | |
"State/Region", | |
"Postal Code", | |
"Country", | |
) | |
mpix_sheets_mapping = { | |
"First Name": "Given Name", | |
"Last Name": "Family Name", | |
"Family Name": "Family Name", | |
"Address Line 1": "Address 1 - Street", | |
"Address Line 2": None, | |
"City": "Address 1 - City", | |
"State/Region": "Address 1 - Region", | |
"Postal Code": "Address 1 - Postal Code", | |
"Country": "Country" | |
} | |
sheets_mpix_mapping = { | |
v: k for k, v in mpix_sheets_mapping.items() if v is not None | |
} | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--input', '-i', required=True) | |
parser.add_argument('--output', '-o', required=False) | |
parser.add_argument('--verbose', '-v', action='count', default=0) | |
args = parser.parse_args() | |
mpix_rows = [mpix_header] | |
with open(args.input) as f: | |
reader = csv.reader(f, delimiter=',') | |
header = next(reader) | |
for row in reader: | |
d = {h: c for h, c in zip(header, row)} | |
mapped_row = tuple( | |
d[mpix_sheets_mapping[h]] if mpix_sheets_mapping.get(h, None) else '' for h in mpix_header | |
) | |
mpix_rows.append(mapped_row) | |
if args.output: | |
with open(args.output, 'w', newline='') as f: | |
writer = csv.writer(f, delimiter=',') | |
for row in mpix_rows: | |
writer.writerow(row) | |
if args.verbose > 0: | |
for row in mpix_rows: | |
print(','.join(row)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment