Saturday, March 17, 2012

Compare to CSV files to see if the rows match.

I needed this for an excel sheet in which I sorted one.
I wanted to double check that I didn't mess up anything when sorting.

 
import csv
origfile=open("C:\\Users\Omar\Desktop\pythonOriginal.csv")
dialect=csv.Sniffer().sniff(origfile.read(1024))
origfile.seek(0)
countorig=0
reader=csv.reader(origfile,dialect)
origlist=[]
for row in reader:
        countorig=countorig+1
        origlist.append('|'.join(row))

        
countcomp=0
complist=[]
compfile=open("C:\\Users\Omar\Desktop\pythonCustom.csv")
readercomp=csv.reader(compfile,dialect)
for row in readercomp:
        countcomp=countcomp+1
        complist.append('|'.join(row))

print (countcomp, " ", countorig )

counti=0
for i in origlist:
        counti=counti+1
        countFound=False
        for j in complist:
                if i==j:
                        countFound=True
                        break
        if countFound==False:
            print("Not Found",counti," ",i)
        else:
            print("Found",counti)

No comments:

Post a Comment