在两个csv文件中查找匹配项

时间:2018-10-20 07:53:44

标签: python csv

我有两个csv文件:第一个有1列,称为ID,共5行,第二个有12列(列5,称为ID),共100行。我试图找到匹配ID,并将整行写入新的csv文件。

谢谢您的帮助!

这是我的代码:

import csv

input_file1 = "/Desktop/New1/file1.csv"
input_file2 = "/Desktop/New1/file2.csv"
output_file = "/Desktop/New1/results.csv"


with open(input_file1) as t1, open(input_file2) as t2:
    fileone = csv.reader(t1)
    filetwo = csv.reader(t2)

    with open(output_file, 'w') as output_res:
        for line in filetwo:
            if line in fileone:
                output_res.write(line)

1 个答案:

答案 0 :(得分:2)

您可以将#include <iostream> using namespace std; int main() { vector<int> v[3]; // here statically i have mentioned size, // create 3 contiguous vectors of type int //remember that it's is 2d kind v[0].push_back(2); v[0].push_back(3); v[1].push_back(3); // This is how you can add more elements than the // specified size-dynamically can change the size // This is the advantage of vector as we can // dynamically add more elements cout<<v[0].front(); //2 cout<<v[0].back(); //3 cout<<v[1].at(0) ; //3 //it's structure is something like this{{2,3},{3},{\0}} return 0; } 中的ID s读入一组,以提高查询效率。您还应该使用file1将行输出为CSV:

csv.writer