比较两个csv文件

时间:2018-09-11 13:44:45

标签: python csv

import csv
with t1 = open('old.csv', 'r') as f1, t2 = open('new.csv', 'r') as f2:
    # skip headers
    next(f1),next(f2)d
    r1 = csv.reader(f1)
    # make set of strings matching format of file2
    st = set("{},{}".format(row[0], row[2]) for row in r1)
    # iterate over every line in file2
    # and check if the line appears in the set
    for line in f2:
        if line.rstrip() in st:
            print(line)

此代码不是我想要的CSV,我想要一个用于比较两个csv文件列的python代码, 如果它们是相似的项目,则我需要创建一个具有与ID相关的相似项目的csv文件。

1 个答案:

答案 0 :(得分:0)

如果可以使用pandas

position: fixed; bottom: 0;

加载其他csv产品

import pandas as pd
df=pd.DataFrame({'sr':[1,2],'keywords':['keyword1','keyword2']}) # can read a csv1 using pd.read_csv('path_to_your_csv1')
df
    keywords    sr
0   keyword1    1
1   keyword2    2

应用搜索条件

df2=pd.DataFrame({'sr':[1,2],'product':['keyword1 is in this product','no key word present']})# can read a csv1 using pd.read_csv2('path_to_your_csv2')
df2
    product                    sr
0   keyword1 is in this product 1
1   no key word present         2

您可以将结果数据帧导出到新的csv。

     product                   sr
0   keyword1 is in this product 1