Python文件匹配

时间:2017-03-01 15:52:45

标签: python-2.7

目前只有一个小任务,我有两个.txt文件,我想找到两者中出现的任何匹配单词,并将结果输出到一个新文件,但是下面的代码只显示空白文件,任何想法是什么我需要改变或看看。

file1 example    file2 example
12345 565        543252 
54321 ff df      12345  
0000  ff f0      11111

理想情况下,12345在两个文件中都匹配,它应该将文件1中的12345 565行打印到输出文件中。

with open('Results.txt', 'r') as file1:
    with open('test.txt', 'r') as file2:
        same = set(file1).intersection(file2)

same.discard('\n')

with open('matches.txt', 'w') as file_out:
    for line in same:
        file_out.write(line)

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您的代码存在的问题是file1的第一行实际上是:

12345 565

file2的第二行仅为:

12345

您的代码无法正常运行,因为intersect会尝试匹配这些不同的行。

你能做的是:

with open('Results.txt', 'r') as file1:
    with open('test.txt', 'r') as file2:
        a = set(x.split()[0] for x in file1)
        b = [x.rstrip() for x in file2]
        same = a.intersection(b)

same.discard('\n')

print same

输出:set(['12345'])

请注意,此方法仅使用file1中的第一列,因此与其他列的任何其他匹配都无法正常工作。希望这有帮助