比较两个文本文件(顺序无关紧要),并将两个文件的共同单词输出到第三个文件



我刚开始编程,我正在尝试比较两个看起来像这样的文件:

file1:
tootsie roll
apple
in the evening
file2:
hello world
do something
apple
output:
"Apple appears x times in file 1 and file 2"

我真的被难住了。我尝试过创建字典、列表、元组、集合,但似乎无法获得我想要的输出。我得到的最接近的结果是输出的行与file1/file2所示的完全一样。

我已经尝试了这里的几个代码片段,但似乎无法得到任何一个来输出我想要的内容。任何帮助都将不胜感激!!

这是我尝试的最后一段代码,它没有给我任何第三个文件的输出。

f1 = open("C:\Users\Cory\Desktop\try.txt", 'r')
f2 = open("C:\Users\Cory\Desktop\match.txt", 'r')
output = open("C:\Users\Cory\Desktop\output.txt", 'w')
file1 = set(f1)
file2 = set(f2)
file(word,freq)
for line in f2:
    word, freq = line.split()
    if word in words:
        output.write("Both files have the following words: " + file1.intersection(file2))
f1.close()
f2.close()
output.close()

您不需要所有这些循环-如果文件很小(即,小于几百MB),您可以更直接地使用它们:

words1 = f1.read().split()
words2 = f2.read().split()
words = set(words1) & set(words2)

则CCD_ 1将是包含那些文件具有共同点的所有单词的CCD_。在拆分文本之前,可以使用lower()忽略大小写。

要对注释中提到的每个单词进行计数,只需使用count()方法:

with open('outfile.txt', 'w') as output:
    for word in words:
        output.write('{} appears {} times in f1 and {} times in f2.n'.format(word, words1.count(word), words2.count(word))

最新更新