Created
February 8, 2013 20:17
-
-
Save darthmall/4741639 to your computer and use it in GitHub Desktop.
Simple Python script for merging rows in a CSV based on some key column.
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 python | |
import csv | |
import sys | |
records = {} | |
keyfield = sys.argv[2] | |
with file(sys.argv[1], 'rU') as f: | |
reader = csv.DictReader(f) | |
for row in reader: | |
rec = records.setdefault(row[keyfield], {}) | |
for k, v in row.iteritems(): | |
if v: | |
rec[k] = v | |
writer = csv.DictWriter(sys.stdout, reader.fieldnames) | |
writer.writeheader() | |
writer.writerows(records.values()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment