Skip to content

Instantly share code, notes, and snippets.

@spellancer
Created June 7, 2017 21:12
Show Gist options
  • Save spellancer/af0255604f2dfad7a995d1437e03722a to your computer and use it in GitHub Desktop.
Save spellancer/af0255604f2dfad7a995d1437e03722a to your computer and use it in GitHub Desktop.
import csv
from collections import OrderedDict
with open('b.csv', 'rb') as f:
r = csv.reader(f)
dict2 = {row[0]: row[1:] for row in r}
with open('a.csv', 'rb') as f:
r = csv.reader(f)
dict1 = OrderedDict((row[0], row[1:]) for row in r)
result = OrderedDict()
for d in (dict1, dict2):
for key, value in d.iteritems():
result.setdefault(key, []).extend(value)
with open('ab_combined.csv', 'wb') as f:
w = csv.writer(f)
for key, value in result.iteritems():
w.writerow([key] + value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment