import csv
import sys

def sanitize(fl):
    return [f.replace('"', "") for f in fl]

def ldcsv(filename):
    lines = list(csv.reader(open(filename)))
    heads = lines[0]
    return [dict(zip(heads, sanitize(l))) for l in lines[1:]]

bks = sum([ldcsv(a) for a in sys.argv[1:]], [])
print len(bks)
print len([b for b in bks if b['Country'] not in ('', 'USA')])

usps = csv.writer(open("out.csv", "w"))
usps.writerow("Full Name,Company,Address 1,Address 2,Address 3,City,State,Zip Code,Province,Country,Urbanization,Phone Number,Fax Number,E Mail,Reference Number,Short Name,".split(","))
# Backer,Email,Pledge,Pledge Status,Pledged At,Notes,Survey Answered,Name,Line_1,Line_2,City,State,Postal Code,Country,Email For Usps Package Tracking
xtract="Name,,Line_1,Line_2,,City,State,Postal Code,,Country,,,,Email For Usps Package Tracking,,Backer,".split(",")
for b in [b for b in bks if b['Country'] not in ('', 'USA')]:
    for f in xtract:
        if '"' in b.get(f, ""):
            print "Quote", b
    usps.writerow([b.get(f, "") for f in xtract])
