【发布时间】:2017-08-29 20:08:26
【问题描述】:
我只是想了解以下脚本中的“keys =”行代码。该脚本被拉入下面链接的答案中。我只想评论那部分,但我没有足够的评论点。
Search Multiple Strings (from File) in a file and print the line
我很难找到关于什么是“key for key”的文档。根据我的发现,它是字典的一部分,但对于理解该部分的任何帮助将不胜感激。谢谢!
keyfile = "keys.txt"
testfile = "test.txt"
keys = [key for key in (line.strip() for line in open(keyfile)) if key]
with open(testfile) as f:
for line in f:
for key in keys:
if key in line:
print(line, end='')
break
【问题讨论】:
标签: python python-2.7