Python:比较两个CSV文件并搜索类似项目 [英] Python: Comparing two CSV files and searching for similar items
问题描述
所以我有两个CSV文件,我想比较,并得到类似的项目的结果。
第一个文件hosts.csv如下所示:
So I've got two CSV files that I'm trying to compare and get the results of the similar items. The first file, hosts.csv is shown below:
Path Filename Size Signature
C:\ a.txt 14kb 012345
D:\ b.txt 99kb 678910
C:\ c.txt 44kb 111213
第二个文件masterlist.csv如下所示:
The second file, masterlist.csv is shown below:
Filename Signature
b.txt 678910
x.txt 111213
b.txt 777777
c.txt 999999
正如你可以看到,行不匹配,masterlist.csv总是大于hosts.csv文件。我想搜索的唯一部分是签名部分。我知道这看起来像:
As you can see the rows do not match up and the masterlist.csv is always larger than the hosts.csv file. The only portion that I'd like to search for is the Signature portion. I know this would look something like:
hosts[3] == masterlist[1]
我正在寻找一个解决方案,将给我一些类似如下(基本上hosts.csv文件与一个新的结果列):
I am looking for a solution that will give me something like the following (basically the hosts.csv file with a new RESULTS column):
Path Filename Size Signature RESULTS
C:\ a.txt 14kb 012345 NOT FOUND in masterlist
D:\ b.txt 99kb 678910 FOUND in masterlist (row 1)
C:\ c.txt 44kb 111213 FOUND in masterlist (row 2)
我搜索了这些帖子,发现了类似这个这里,但我不太明白,因为我还在学习python。
I've searched the posts and found something similar to this here but I don't quite understand it as I'm still learning python.
编辑使用Python 2.6
Edit Using Python 2.6
推荐答案
您可以找到python CSV模块的文档此处。
You can find the documentation for the python CSV module here.
您正在寻找的是这样的:
What you're looking for is something like this:
import csv
f1 = file('hosts.csv', 'r')
f2 = file('masterlist.csv', 'r')
f3 = file('results.csv', 'w')
c1 = csv.reader(f1)
c2 = csv.reader(f2)
c3 = csv.writer(f3)
masterlist = list(c2)
for hosts_row in c1:
row = 1
found = False
for master_row in masterlist:
results_row = hosts_row
if hosts_row[3] == master_row[1]:
results_row.append('FOUND in master list (row ' + str(row) + ')')
found = True
break
row = row + 1
if not found:
results_row.append('NOT FOUND in master list')
c3.writerow(results_row)
f1.close()
f2.close()
f3.close()
这篇关于Python:比较两个CSV文件并搜索类似项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!