user3514648
user3514648

Reputation: 101

csv file comparison using python

I tried this script to compare two csv files:

import csv

file1 = open("1.csv", "r")
reader1 = csv.reader(file1)
reader1.next()
file2 = open("2.csv", "r")
reader2 = csv.reader(file2)
reader2.next()
file3 = open("file3.txt", "w")
file4 = open("file4.txt", "w")
file1.seek(0, 0)
file2.seek(0, 0)
list1 = file1.readlines()
list2 = file2.readlines()
for i in list1:
    for j in list2:
        if i == j:
            file3.write(i)
            file3.write(j)
        else:
            file4.write(i)
            file4.write(j)
    continue

and the output I am getting with headers included and plus the mismatched files are repeating. for eg, if my 1.csv contains

Name    Salary   
A       20000
B       15000
C       10000

2.csv contains

Name    Salary
A       40000
D       10000
B       15000
C        9000

output should be

file3: B 15000 B 15000

file4: A 20000 A 40000
       C 10000 C 9000

------(no D in 1.csv) D 10000

Upvotes: 1

Views: 135

Answers (1)

Klaus-Dieter Warzecha
Klaus-Dieter Warzecha

Reputation: 2325

Wouldn't it be easier to compare dictionaries with the names as keys and the salaries as values? One way to populate the dict could be:

import csv

csv.register_dialect('spaces', delimiter = ' ')

salaries1 = {}
with open('l1.csv') as l1:
   reader1 = csv.reader(l1, dialect='spaces')
   reader1.next()    # skip header

   salaries1 = {row[0]:row[1] for row in reader1}

 print salaries1

Upvotes: 1

Related Questions