# file1.txt
# dog
# cat
# fish
# file2.txt
# The cat ran
# The fish swam
# The parrot sang
# reading file1 and getting the keywords
with open("file1.txt") as f:
key_words = set(f.read().splitlines())
# reading file2 and iterating over all read lines
with open("file2.txt") as f:
all_lines = f.read().splitlines()
for line in all_lines:
if any(kw in line for kw in key_words): # if any of the word in key words is in line print it
print(line)
像这样的怎么样。我们从第一个文件中提取关键字并存储它们,然后在阅读第二个文件时在打印之前引用它们
相关问题 更多 >
编程相关推荐