Python 在txt文件中搜索字符串/else打印不存在
我遇到了一个问题,我正试图编写一个程序,梳理“某些”搜索词的配置文件,如果匹配,则打印“它在那里”,如果不匹配,则打印“它不在这里”。以下是我到目前为止的情况:Python 在txt文件中搜索字符串/else打印不存在,python,for-loop,if-statement,search,text,Python,For Loop,If Statement,Search,Text,我遇到了一个问题,我正试图编写一个程序,梳理“某些”搜索词的配置文件,如果匹配,则打印“它在那里”,如果不匹配,则打印“它不在这里”。以下是我到目前为止的情况: import sys import fnmatch import re check = ["test1", "test2", "test3"] for f in filter(os.path.isfile, sys.argv[1:]): ##open doc arg for line in open(f).readline
import sys
import fnmatch
import re
check = ["test1", "test2", "test3"]
for f in filter(os.path.isfile, sys.argv[1:]): ##open doc arg
for line in open(f).readlines(): ##loop for reading line by line
if re.match(check[0], line): ##match at beginning for check
print(check[0], "is in place") ##print if match == true
elif re.search(check[0], line): ##if not check search (full file)
print(check[0], "is not in place") ##print if true
for line in open(f).readlines():
if re.match(check[1], line):
print(check[1], "is in place")
elif ((re.search(check[1], line)) == None):
print(check[1], "is not in place")
所以问题是,如果我打印一条else语句,那么每行(全部1500行)都会打印,因为循环是逐行运行的。有没有办法不逐行搜索整个文档?要读取整个文件,可以使用
read()
而不是readlines()
如果您在文件中查找的只是一个字符串,则不需要re
:
if check[0] in lines:
print(check[0], "is in place")
要读取整个文件,可以使用
read()
而不是readlines()
如果您在文件中查找的只是一个字符串,则不需要re
:
if check[0] in lines:
print(check[0], "is in place")
是的,这是可能的,使用
read()
。但是要注意,如果您的文件很大,那么在内存中一次性加载整个文件可能不是一个好主意
此外,如果要多次循环浏览同一个文件,请尝试通过仅在该文件上迭代一次并同时搜索check
数组中的所有值来避免这种情况。此外,尽可能避免使用正则表达式,因为它们可能很慢。类似的方法也可以奏效:
for line in open(f).readlines():
for check_value in check:
if check_value in line:
print "{} is in place.".format(check_value)
是的,这是可能的,使用
read()
。但是要注意,如果您的文件很大,那么在内存中一次性加载整个文件可能不是一个好主意
此外,如果要多次循环浏览同一个文件,请尝试通过仅在该文件上迭代一次并同时搜索check
数组中的所有值来避免这种情况。此外,尽可能避免使用正则表达式,因为它们可能很慢。类似的方法也可以奏效:
for line in open(f).readlines():
for check_value in check:
if check_value in line:
print "{} is in place.".format(check_value)
将
for
循环的else
子句与break
语句一起使用。还要注意的是,只需迭代文件本身即可;无需显式读取所有行。(我还添加了带有的,以确保文件关闭。)
您甚至可以将其编写为那些曾经流行的生成器表达式之一:
with open(f) as infile:
if any(re.match(check[0], line) for line in infile):
print(check[0], "is in place")
else:
print(check[0], "is not in place")
由于打印的消息非常相似,您可以对其进行进一步编码:
with open(f) as infile:
print(check[0], "is" if any(re.match(check[0], line) for line in infile) else "is not", "in place")
将for
循环的else
子句与break
语句一起使用。还要注意的是,只需迭代文件本身即可;无需显式读取所有行。(我还添加了带有
的,以确保文件关闭。)
您甚至可以将其编写为那些曾经流行的生成器表达式之一:
with open(f) as infile:
if any(re.match(check[0], line) for line in infile):
print(check[0], "is in place")
else:
print(check[0], "is not in place")
由于打印的消息非常相似,您可以对其进行进一步编码:
with open(f) as infile:
print(check[0], "is" if any(re.match(check[0], line) for line in infile) else "is not", "in place")
我想您可以将文件读入字符串,并使用一个简单的如果x在…
,即:
with open("text_contains.txt") as f:
text = f.read().lower() # remove .lower() for caseSensiTive matching
for x in ["test1", "test2", "test3"]:
if x in text:
print("{} is in place".format(x))
else:
print("{} is not in place".format(x))
我想您可以将文件读入字符串,并使用一个简单的如果x在…
,即:
with open("text_contains.txt") as f:
text = f.read().lower() # remove .lower() for caseSensiTive matching
for x in ["test1", "test2", "test3"]:
if x in text:
print("{} is in place".format(x))
else:
print("{} is not in place".format(x))
如果您确实需要逐行读取文件(我假设您需要引用的行),那么:
但是,如果您只需要检查术语是否存在,而不考虑行,则只需使用立即读取整个文件:
for f in filter(os.path.isfile, sys.argv[1:]):
with open(f) as file:
text = file.read()
for term in searchTerms:
if re.match(term, text):
print("'%s' found" % term)
else:
print("'%s' not found" % term)
如果您确实需要逐行读取文件(我假设您需要引用的行),那么:
但是,如果您只需要检查术语是否存在,而不考虑行,则只需使用立即读取整个文件:
for f in filter(os.path.isfile, sys.argv[1:]):
with open(f) as file:
text = file.read()
for term in searchTerms:
if re.match(term, text):
print("'%s' found" % term)
else:
print("'%s' not found" % term)