Created
January 23, 2019 07:36
-
-
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
This file contains hidden or 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
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