比较两个 CSV 文件并搜索相似的项目

编程入门 行业动态 更新时间:2024-10-14 06:19:44
本文介绍了比较两个 CSV 文件并搜索相似的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

所以我有两个 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]

我正在寻找一种解决方案,它可以为我提供如下内容(基本上是带有新 RESULTS 列的 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

推荐答案

虽然我的解决方案工作正常,但请查看下面 Martijn 的答案以获得更有效的解决方案.

While my solution works correctly, check out Martijn's answer below for a more efficient solution.

您可以在此处找到 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()

更多推荐

比较两个 CSV 文件并搜索相似的项目

本文发布于:2023-11-30 16:58:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650785.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两个   文件   项目   CSV

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!