Python/CSV unique rows with unique values per row in a column
<p>Have this dataset, dataset is fictional:</p>

<pre><code>cat sample.csv 

id,fname,lname,education,gradyear,attributes
"6F9619FF-8B86-D011-B42D-00C04FC964FF",john,smith,mit,2003,qa
"6F9619FF-8B86-D011-B42D-00C04FC964FF",john,smith,harvard,2007,"test|admin,test"
"6F9619FF-8B86-D011-B42D-00C04FC964FF",john,smith,harvard,2007,"test|admin,test"
"6F9619FF-8B86-D011-B42D-00C04FC964FF",john,smith,ft,2012,NULL
"6F9619FF-8B86-D011-B42D-00C04FC964F1",john,doe,htw,2000,dev
</code></pre>

<p>When I run this script, which parses the csv and finds unique rows, concating rows in on column when more are found:</p>

<p>parse-csv.py</p>

<pre><code>import itertools

from itertools import groupby
import csv
import pprint
import argparse

if __name__ == '__main__':
        parser = argparse.ArgumentParser(description='sql dump parser')
        parser.add_argument('-i','--input', help='input file', required=True)
        parser.add_argument('-o','--output', help='output file', required=True)
        args = parser.parse_args()
        inputf = args.input
        outputf = args.output


t = csv.reader(open(inputf, 'rb'))
t = list(t)


def join_rows(rows):
    return [(e[0] if i &lt; 1 else '|'.join(e)) for (i, e) in enumerate(zip(*rows))]

myfile = open(outputf, 'wb')
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL, lineterminator='\n')

for name, rows in groupby(t, lambda x:x[0]):
    wr.writerow(join_rows(rows))
    #print join_rows(rows)
</code></pre>

<p>And than another script, which makes sure each colum has only unique values separated by "|"</p>

<p>unique.py</p>

<pre><code>import csv
import sys
from collections import OrderedDict
import argparse

csv.field_size_limit(sys.maxsize)

import argparse

if __name__ == '__main__':
        parser = argparse.ArgumentParser(description='sql dump parser - unique')
        parser.add_argument('-i','--input', help='input file', required=True)
        parser.add_argument('-o','--output', help='output file', required=True)
        args = parser.parse_args()
        inputf = args.input
        outputf = args.output


with open(inputf) as fin, open(outputf, 'wb') as fout:
    csvin = csv.DictReader(fin)
    csvout = csv.DictWriter(fout, fieldnames=csvin.fieldnames, quoting=csv.QUOTE_ALL,lineterminator='\n')
    csvout.writeheader()
    for row in csvin:
        for k, v in row.items():
            row[k] = '|'.join(OrderedDict.fromkeys(v.split('|')))
        csvout.writerow(row)
</code></pre>

<p>It works for the sample.csv</p>

<p>Output:</p>

<pre><code>$ python parse-csv.py -i sample.csv -o sample-out.csv
$ python unique.py -i sample-out.csv -o sample-final.csv

$ cat sample-final.csv

"id","fname","lname","education","gradyear","attributes"
"6F9619FF-8B86-D011-B42D-00C04FC964FF","john","smith","mit|harvard|ft","2003|2007|2012","qa|test|admin,test|NULL"
"6F9619FF-8B86-D011-B42D-00C04FC964F1","john","doe","htw","2000","dev"
</code></pre>

<p>But when I do the same for this:</p>

<p>(dataset is fictional)</p>

<p>sample2.csv</p>

<pre><code>id,lastname,firstname,middlename,address1,address2,city,zipcode,city2,zipcode2,emailaddress,website
"E387F3C1-F6E9-40DD-86AB-A7149C67F61C","Technical Support",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL
"648EEB5D-0586-444A-B86F-4EB2446BBC93","Palm","Samuel","J",NULL,NULL,NULL,NULL,NULL,NULL,"",NULL
"A94FAD4E-27DB-48FE-B89E-C37B408C5DD5","Mait","A.V.",NULL,NULL,NULL,NULL,NULL,NULL,NULL,"mait@yahoo.com",NULL
"E387F3C1-F6E9-40DD-86AB-A7149C67F61C","Technical Support",NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL
"648EEB5D-0586-444A-B86F-4EB2446BBC93","Palm","Samuel","J",NULL,NULL,NULL,NULL,NULL,NULL,"",NULL
"A94FAD4E-27DB-48FE-B89E-C37B408C5DD5","Mait","A.V.",NULL,NULL,NULL,NULL,NULL,NULL,NULL,"mait@yahoo.com",NULL
"FDFCA22A-EE19-4997-B892-90B2006FE328","Drago","Paul",NULL,"","","","",NULL,NULL,"psd@gmail.com",NULL
"FDFCA22A-EE19-4997-B892-90B2006FE328","Drago","Paul",NULL,"","","","",NULL,NULL,"psd@gmail.com",NULL
"FDFCA22A-EE19-4997-B892-90B2006FE328","Drago","Paul",NULL,"","","","",NULL,NULL,"psd@gmail.com",NULL
"FDFCA22A-EE19-4997-B892-90B2006FE328","Drago","Paul",NULL,"","","","",NULL,NULL,"psd@gmail.com",NULL
"FDFCA22A-EE19-4997-B892-90B2006FE328","Drago","Paul",NULL,"","","","",NULL,NULL,"psd@gmail.com",NULL
</code></pre>

<p>The output is:</p>

<pre><code>$ python parse-csv.py -i sample2.csv -o sample2-out.csv
$ python unique.py -i sample2-out.csv -o sample2-final.csv
$ cat sample2-final.csv

"id","lastname","firstname","middlename","address1","address2","city","zipcode","city2","zipcode2","emailaddress","website"
"E387F3C1-F6E9-40DD-86AB-A7149C67F61C","Technical Support","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL"
"648EEB5D-0586-444A-B86F-4EB2446BBC93","Palm","Samuel","J","NULL","NULL","NULL","NULL","NULL","NULL","","NULL"
"A94FAD4E-27DB-48FE-B89E-C37B408C5DD5","Mait","A.V.","NULL","NULL","NULL","NULL","NULL","NULL","NULL","mait@yahoo.com","NULL"
"E387F3C1-F6E9-40DD-86AB-A7149C67F61C","Technical Support","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL","NULL"
"648EEB5D-0586-444A-B86F-4EB2446BBC93","Palm","Samuel","J","NULL","NULL","NULL","NULL","NULL","NULL","","NULL"
"A94FAD4E-27DB-48FE-B89E-C37B408C5DD5","Mait","A.V.","NULL","NULL","NULL","NULL","NULL","NULL","NULL","mait@yahoo.com","NULL"
"FDFCA22A-EE19-4997-B892-90B2006FE328","Drago","Paul","NULL","","","","","NULL","NULL","psd@gmail.com","NULL"
</code></pre>

<p>Why it doesn't properly get unique rows and columns like it did for sample.csv???? </p>

<p>Anybody has any ideas?</p>

<p>Thanks in advance! Chewing on this for a long time now ....</p>