Skip to content

Instantly share code, notes, and snippets.

@sidharthkuruvila
Created November 5, 2019 11:20
Show Gist options
  • Save sidharthkuruvila/eb09eeddd28559ec668e455b52930959 to your computer and use it in GitHub Desktop.
Save sidharthkuruvila/eb09eeddd28559ec668e455b52930959 to your computer and use it in GitHub Desktop.
import csv
import itertools
l = list(csv.DictReader(open('Mailchimp_GeoTwitter.csv')))
col_email = 'Email Address'
col_address = 'Your Mailing Address (where you receive your rock)'
col_shipping = 'Are you willing to ship internationally?'
col_first_name = 'First Name'
col_last_name = 'Last Name'
col_rock_type = "Any rock type you'd like to add to your collection"
col_twitter_handle = 'Your twitter handle (optional)'
is_international = {'No, I do not want to ship internationally': False, 'Yes, but I prefer domestic': False, 'Yes, I prefer international': True }
nl = [(e[col_email], e[col_address].split(' ')[-1], is_international[e[col_shipping]]) for e in l if e [col_address] != '']
for_email = dict((e[col_email], e) for e in l)
nnl = [('international' if e[2] else e[1], e[0]) for e in nl]
grouped = [list(e[1] for e in j[1]) for j in itertools.groupby(sorted(nnl), lambda e: e[0])]
def loop(l): return list(zip(l, l[1:] + [l[0]]))
email_match = list(itertools.chain(*[loop(e) for e in grouped]))
def gen():
for src, dest in email_match:
d = dict(for_email[dest])
yield {
"Gifter": src,
"Giftee": dest,
"Giftee's First Name": d[col_first_name],
"Giftee's Last Name": d[col_last_name],
"Giftee's Twitter Handle": d[col_twitter_handle],
"Giftee's Address": d[col_address],
"Giftee's Rock Preference": d[col_rock_type]
}
ol = list(gen())
with open('out.csv', 'w') as fo:
writer = csv.DictWriter(fo, ol[0].keys())
writer.writeheader()
for row in ol:
writer.writerow(row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment