我试过这个脚本来比较两个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
答案 0 :(得分:0)
试试这个。有用 。我向你展示了一种搜索机制。你可以修改以提高效率
import csv
file1 = open("Book1.csv", "r")
reader1=csv.reader(file1)
print reader1
reader1.next()
file2 = open("Book2.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()
print list1
list2 = file2.readlines()
print list2
d={}
for j in list2:
temp=j.split(',')
print temp
d[temp[0]]=temp[1]
print d
for i,j in zip(list1,list2):
key = i.split(',')
try:
if d[key[0]]:
print d[key[0]]
if d[key[0]]==key[1]:
file3.write(i)
file3.write(j)
else:
file4.write(i)
file4.write(j)
except:
print key[0],'not in file 2'
continue