Python 3.x 执行多行搜索
我当前的脚本正在运行(见下文),但在单行上匹配。如何匹配多行Python 3.x 执行多行搜索,python-3.x,Python 3.x,我当前的脚本正在运行(见下文),但在单行上匹配。如何匹配多行 for filename in os.listdir(path): with open(os.path.join(path,filename), "r") as infile: check1 = "fail" check2 = "fail" for line in infile: if line.startswith("logging buffered 1
for filename in os.listdir(path):
with open(os.path.join(path,filename), "r") as infile:
check1 = "fail"
check2 = "fail"
for line in infile:
if line.startswith("logging buffered 16384"):
check1 = 'pass'
if line.startswith("logging console informational"):
check2 = 'pass'
fh.write("{},{},{}\n".format(filename, check1, check2))
fh.close()
如果
文件中存在检查
中的所有项目,则变量结果
的值将为True
checks = ["line1", "line2"]
file = ["line3", "line4", line5", "line1", "line2"]
result = (all([True for line in checks if line in file]))
将要搜索的行定义为列表,并在文件中运行上述代码。什么叫“匹配多行”?我想为2行或更多行创建一个匹配。假设我有一行写着“这是line1”,第二行写着“这是line2”。我希望脚本在文件中搜索这两行并返回“通过”或“失败”。