在python中检查文本文件中的某些字符串
我正在用python编写一个代码,它将在数千个文本文件中查找特定字符串,然后将这些文本文件的名称附加到两个列表中的一个。我尝试使用带有多个参数的if语句来完成此操作,如下所示:在python中检查文本文件中的某些字符串,python,string,file,search,text,Python,String,File,Search,Text,我正在用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
# 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语句中有更多的参数,但由于某种原因,当我得到包含“this phrase”或“that phrase”的文本文件列表时,其中一些还包含“not this phrase”。为什么会这样?是因为我在if语句中使用了太多参数吗?该程序的主要目标是将文本文件名附加到一个或另一个列表中,具体取决于
True
或False
是否返回到主函数。您需要正确地分组条件,例如:
if (
("this phrase" in textfile or "that phrase" in textfile) and not (
"not this phrase" in textfile or "not that phrase" in textfile)
):
return True
同意尼克的回答。但您说过if语句中还有很多其他参数,所以您不想在if循环中编写所有这些语句 我建议使用两个列表。下面是一些示例代码 注意:请记住,这是一个快速而肮脏的解决方案。您可以通过使用生成器而不是列表(如果您的模式计数很高),使用lambda函数来减少行数(尽管看起来很复杂),等等,根据您的偏好来实现这一点
contain_lst = ['pattern1', 'p2', 'p3']
not_contain_lst = ['ncp1', 'ncp2', 'ncp3', 'ncp4']
for each_file in files_list:
with open(each_file) as f:
data = f.read()
contain_match = 1
for each_contain_pattern in contain_lst:
if each_contain_pattern in data:
contain_match = 0
not_contain_match = 0
for each_not_contain_pattern in not_contain_lst:
if each_not_contain_pattern in data:
not_contain_match = 1
if contain_match and not not_contain_match:
print "File %s has all the needed patterns and doesn't have the not needed patterns" % each_file
else:
print "ERROR- File %s doesn't match the requirements" % each_file
它是一个字符串,包含文本文件中的信息。在这里,我将快速编辑问题。