比较两个csv文件和获得差异 [英] Comparing two csv files and getting difference
问题描述
我有两个csv文件,我需要比较,然后吐出差异:
CSV FORMAT:
名称产品编号
Adam Apple 5
Tom Orange 4
Adam Orange 11
我需要比较两个csv文件,然后告诉我,如果在工作表和工作表2上的Adams苹果之间有区别,并为所有的名字和产生数字。
任何指针都将非常感谢。
如果你的CSV文件不是这么大,他们会把你的机器膝盖,如果你加载到内存,那么你可以尝试像:
import csv
csv1 = list(csv.DictReader(open('file1.csv')))
csv2 = list(csv.DictReader(open file2.csv')))
set1 = set(csv1)
set2 = set(csv2)
print set1 - set2#in 1,not in 2
print set2 - set1 #in 2,not in 1
print set1&对于大型文件,您可以将它们加载到SQLite3数据库中,并使用SQL查询来执行SQLite3数据库。做相同的,或者通过相关的键排序,然后做一个匹配合并。
I have two csv file I need to compare and then spit out the differnces:
CSV FORMAT:
Name Produce Number
Adam Apple 5
Tom Orange 4
Adam Orange 11
I need to compare the two csv files and then tell me if there is a difference between Adams apples on sheet and sheet 2 and do that for all names and produce numbers. Both CSV files will be formated the same.
Any pointers will be greatly appreciated
解决方案 If your CSV files aren't so large they'll bring your machine to its knees if you load them into memory, then you could try something like:
import csv
csv1 = list(csv.DictReader(open('file1.csv')))
csv2 = list(csv.DictReader(open('file2.csv')))
set1 = set(csv1)
set2 = set(csv2)
print set1 - set2 # in 1, not in 2
print set2 - set1 # in 2, not in 1
print set1 & set2 # in both
For large files, you could load them into a SQLite3 database and use SQL queries to do the same, or sort by relevant keys and then do a match-merge.
这篇关于比较两个csv文件和获得差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!