【发布时间】:2016-03-08 19:47:41
【问题描述】:
我正在编写一个 Python 代码,该代码将在数千个文本文件中查找某些字符串,然后将这些文本文件的名称附加到两个列表之一。我正在尝试使用带有多个参数的 if 语句来做到这一点:
# up here would be other code
#
with open("/home/textfile.txt", 'r') as f:
textfile = f.read()
if "this phrase" in textfile or "that phrase" in textfile and not "not this phrase" in textfile and not "not that phrase" in textfile:
return True
elif "not this phrase" in textfile or "not that phrase" in textfile:
return False
现在在我的代码中,这些 if 语句中有更多参数,但由于某种原因,当我得到包含“这个短语”或“那个短语”的文本文件列表时,其中一些还包含“不是这个短语” .为什么会这样?是因为我在 if 语句中使用了太多参数吗?该程序的主要目标是将文本文件名附加到一个列表或另一个列表中,具体取决于是否将True 或False 返回给主函数。
【问题讨论】:
-
这是一个包含文本文件信息的字符串。在这里,我将快速编辑问题。
标签: python string file search text