比较两个csv文件Python

时间:2014-02-12 03:50:08

标签: python

我有两个csv文件..我想比较file1的第二列和第三列,然后在outputfile中打印匹配

file1.csv

A0AUZ9 ENSP00000281772

A0AUZ9 ENSP00000405724

A0AUZ9 ENSP00000401408

A0AUZ9 ENSP00000393432

A0AVK6 ENSP00000250024

A0AVK6 ENSP00000434199

A0AVT1 ENSP00000313454

A0AVT1 ENSP00000399234

A0JNW5 ENSP00000279907

file2.csv

meNOG13950 9606 ENSP00000305973 1 264

meNOG14003 9606 ENSP00000281772 1 987

meNOG14125 9606 ENSP00000346414 1 624

meNOG06168 9606 ENSP00000287936 1 888

meNOG06177 9606 ENSP00000313454 1 1052

meNOG06177 9606 ENSP00000333266 1 1012

outputfile.txt

meNOG14003 9606 ENSP00000281772 1 987

meNOG06177 9606 ENSP00000313454 1 1052

感谢

2 个答案:

答案 0 :(得分:1)

可能是这样的:

import csv
with open('file1.csv') as f1, open('file2.csv') as f2:
    reader1, reader2 = [csv.reader(f, delimiter=' ') for f in [f1,f2]]
    m1 = {row[1]:row for row in reader1}
    m2 = {row[2]:row for row in reader2}
    intersect = set(m1.keys()) & set(m2.keys())
    with open('output.txt', 'w') as f_out:
        writer = csv.writer(f_out, delimiter=' ')
        for k in intersect:
            writer.writerow(m2[k])

输出:

$ cat output.txt
meNOG14003 9606 ENSP00000281772 1 987
meNOG06177 9606 ENSP00000313454 1 1052

答案 1 :(得分:0)

你的代码在哪里......

def do_work():
    with open('test.csv') as f:
        for line in f:
            line = line.rstrip("\n")
            column = line.split(",")
            if column[1] == column[2]:
                print("Woohoo, {0} is equal to {1}!".format(column[1], column[2]))

do_work()

这就是如何在同一个csv中比较第2列和第3列,所以建立起来。打开两个处理程序并比较CSV。