【发布时间】:2016-10-14 21:25:43
【问题描述】:
高级要求是我需要一个等效的“grep -f match_str_file search_file”。
我有一个字符串列表,我需要在给定的文本文件中找到这些字符串。 该字符串可以出现在文件的任何行中的任何位置。
什么是实现这一目标的有效方法?
matchstr = ['string1',
'string2',
'string3',
...
'string1000']
with open('some_text_file') as file:
for line in file:
if <any matchstr> in line:
print( 'Found a match!', matchstr[x])
【问题讨论】:
-
我不明白。为什么这个问题被否决了?做一个简单的问题并不会弄错。如有其他原因,应注明。
-
您的“效率”指标是什么。你尝试过什么“低效”的方法?为什么它不起作用。
-
当有人投出一票接近的票时,您应该阅读该消息。它通常会很好地解释随之而来的反对票。
标签: python python-3.x string-matching