[英]Python - Compare similar values in two columns from two different csv
I have two CSV files with the same row names: 我有两个具有相同行名的CSV文件:
Name, Lastname.
However, file2.csv has an extra column named 但是,file2.csv有一个名为
Attention
Each file has a different list of name and lastname (not in order). 每个文件都有一个不同的名称和姓氏列表(顺序不一)。 I'm trying to find a way to find to print the attention column, if the name and lastname are in both files.
我正在尝试找到一种方法来打印注意栏,如果名称和姓氏都在两个文件中。
This is what I have so far: 这是我到目前为止的内容:
with open('result.csv') as r:
set1 = set(x[0] for x in csv.reader(r))
with open('result2.csv') as r:
set2 = set(x[0] for x in csv.reader(r))
for x, y in zip(set1, set2):
if x[0] == y[0]:
print("Matched")
How can I read the first and second column for each file? 如何读取每个文件的第一和第二列?
Thank you 谢谢
edit: being more clear 编辑:更清晰
Create a set of tuples for the lookup table which will contain the values from your first two columns, eg: 为查找表创建一组元组,其中将包含前两列中的值,例如:
with open("result.csv", "r") as f:
result = {(x[0], x[1]) for x in csv.reader(f)}
And then just iterate over the second file, check if the tuple of the first two columns exists in the lookup table from the first file, and if yes - print the third column on each match, eg: 然后仅遍历第二个文件,检查第一个文件的查找表中是否存在前两列的元组,如果是,则在每个匹配项上打印第三列,例如:
with open("result2.csv", "r") as f:
for row in csv.reader(f):
if (row[0], row[1]) in result:
print("Matched: {}".format(row[2])) # print the third column
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.