Skip to content

Instantly share code, notes, and snippets.

@bchess
Created January 23, 2019 07:36
Show Gist options
  • Save bchess/dd4ee65aa7967402472c35935e1cb6c6 to your computer and use it in GitHub Desktop.
Save bchess/dd4ee65aa7967402472c35935e1cb6c6 to your computer and use it in GitHub Desktop.
Re-format a bunch of CSVs into one giant CSV with all fields
import csv
import sys
all_keys = []
for filename in sys.argv[1:]:
with open(filename) as csvfile:
reader = csv.DictReader(csvfile)
for new_key in reader.fieldnames:
if new_key not in all_keys:
all_keys.append(new_key)
writer = csv.DictWriter(sys.stdout, all_keys)
writer.writeheader()
for filename in sys.argv[1:]:
with open(filename) as csvfile:
reader = csv.DictReader(csvfile)
expanded_row = dict.fromkeys(all_keys, '')
for row in reader:
expanded_row.update(row)
writer.writerow(expanded_row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment