1

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
0

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Klaus i just gave a small example in my real time requirement der will be more columns to compare

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.