Last Updated: February 25, 2016
·
2.171K
· lukasz-madon

Intersection and other operations on CSV file

I have sent an email campaign to the wrong list, so to get a sense of what's going on (who opened it, who clicked) I made this little script

import csv


a = set()
with open('a.csv', 'rb') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=',', quotechar='|')
    for r in spamreader:
        a.add((r[0],r[1],r[2]))   # I only needed email, first and last name


b = set()
with open('b.csv', 'rb') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=',', quotechar='|')
    for r in spamreader:
        b.add((r[0],r[1],r[2]))

intersection = a & b

for x in intersection:
    print ",".join(x)

Since they are represented as sets you can do subtraction, union etc.