使用python进行csv文件比较

时间:2014-04-26 05:52:57

标签: python file csv comparison

我尝试使用此脚本来比较两个csv文件:

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

和包含标题的输出以及不匹配的文件正在重复。例如,如果我的1.csv包含

Name    Salary   
A       20000
B       15000
C       10000

2.csv包含

Name    Salary
A       40000
D       10000
B       15000
C        9000

输出应该是

file3: B 15000 B 15000

file4: A 20000 A 40000
       C 10000 C 9000

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

1 个答案:

答案 0 :(得分:1)

将字典与作为键的名称和作为值的工资进行比较会更容易吗?填充dict的一种方法可能是:

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