为什么我的for循环无法捕获"success"?
Why is my for loop unable to capture "success"?
我正在尝试扫描日志文件并匹配某些字符串(运算符)。匹配运算符后,我想寻找另一个字符串,该字符串将在日志文件中指定 "success" 任务。我的循环只在第一个 elif 之前工作。变量 operator_type 定义正确。我通过打印 operator_type 来测试它。我不完全理解使用 "break" 所以这些可能是问题所在。代码一直跳到 else(根据我的输出)。
fl_sts == 'FAILED'
我的代码能够发现故障。它无法找到成功。
for i, line in enumerate(lines):
if (fl_sts in line):
end_line = lines[i]
success_status = fl_sts
break
elif operator_type == op_py in line:
for line in lines:
if (sc_crt_py in line):
end_line = lines[-1]
success_status = sc_sts
else:
break
elif operator_type == op_eml in line:
for line in lines:
if (sc_crt_eml in line):
end_line = lines[-1]
success_status = sc_sts
else:
break
elif operator_type == op_s3 in line:
for line in lines:
if (sc_crt_s3 in line):
end_line = lines[-1]
success_status = sc_sts
else:
break
elif operator_type == op_bsh in line:
for line in lines:
if (sc_crt_bsh in line):
end_line = lines[-1]
success_status = sc_sts
else:
break
elif operator_type == op_snw in line:
for line in lines:
if (sc_crt_snw in line):
end_line = lines[-1]
success_status = sc_sts
else:
break
elif operator_type == op_snw_sns in line:
for line in lines:
if (sc_crt_snw_sns in line):
end_line = lines[-1]
success_status = sc_sts
else:
break
else:
success_status = sc_sts_rn
end_line = 'None'
例如,如果 operator_type 是 (PythonOperator)
,则 sc_crt_py
是 'Done. Returned value was: None'
。
示例行[2019-07-08 15:06:44,415] {python_operator.py:104} INFO - Done. Returned value was: None
。
请注意 (PythonOperator)
在此循环上方成功捕获,{python_operator}
是线路上的噪音。
我期望 Successful
的输出为 sc_sts = 'Succesful'
我很欣赏见解。我还在学习中。
删除中断,您将在第一个 If
之后退出循环
你不理解的情绪break
似乎是正确的。 break
中断 最内层的封闭 for 或 while 循环。它用于提前停止循环的执行。我会通读有关流量控制的文档:https://docs.python.org/3/tutorial/controlflow.html
with open(path, "r") as f:
started = None
for line in f.readlines():
line.strip()
if started is None:
if operator in line:
#Do something here
else:
if "success" in line:
#Do something here
started = None
else:
pass
这对我有用。删除中断并更改 elif 语句。
for i, line in enumerate(lines):
if (fl_sts in line):
end_line = lines[i]
success_status = fl_sts
break
elif operator_type == op_py:
for line in lines:
if (sc_crt_py in line):
end_line = lines[-1]
success_status = sc_sts
elif operator_type == op_eml:
for line in lines:
if (sc_crt_eml in line):
end_line = lines[-1]
success_status = sc_sts
elif operator_type == op_s3:
for line in lines:
if (sc_crt_s3 in line):
end_line = lines[-1]
success_status = sc_sts
elif operator_type == op_bsh:
for line in lines:
if (sc_crt_bsh in line):
end_line = lines[-1]
success_status = sc_sts
elif operator_type == op_snw:
for line in lines:
if (sc_crt_snw in line):
end_line = lines[-1]
success_status = sc_sts
elif operator_type == op_snw_sns:
for line in lines:
if (sc_crt_snw_sns in line):
end_line = lines[-1]
success_status = sc_sts
else:
success_status = sc_sts_rn
end_line = 'None'
我正在尝试扫描日志文件并匹配某些字符串(运算符)。匹配运算符后,我想寻找另一个字符串,该字符串将在日志文件中指定 "success" 任务。我的循环只在第一个 elif 之前工作。变量 operator_type 定义正确。我通过打印 operator_type 来测试它。我不完全理解使用 "break" 所以这些可能是问题所在。代码一直跳到 else(根据我的输出)。
fl_sts == 'FAILED'
我的代码能够发现故障。它无法找到成功。
for i, line in enumerate(lines):
if (fl_sts in line):
end_line = lines[i]
success_status = fl_sts
break
elif operator_type == op_py in line:
for line in lines:
if (sc_crt_py in line):
end_line = lines[-1]
success_status = sc_sts
else:
break
elif operator_type == op_eml in line:
for line in lines:
if (sc_crt_eml in line):
end_line = lines[-1]
success_status = sc_sts
else:
break
elif operator_type == op_s3 in line:
for line in lines:
if (sc_crt_s3 in line):
end_line = lines[-1]
success_status = sc_sts
else:
break
elif operator_type == op_bsh in line:
for line in lines:
if (sc_crt_bsh in line):
end_line = lines[-1]
success_status = sc_sts
else:
break
elif operator_type == op_snw in line:
for line in lines:
if (sc_crt_snw in line):
end_line = lines[-1]
success_status = sc_sts
else:
break
elif operator_type == op_snw_sns in line:
for line in lines:
if (sc_crt_snw_sns in line):
end_line = lines[-1]
success_status = sc_sts
else:
break
else:
success_status = sc_sts_rn
end_line = 'None'
例如,如果 operator_type 是 (PythonOperator)
,则 sc_crt_py
是 'Done. Returned value was: None'
。
示例行[2019-07-08 15:06:44,415] {python_operator.py:104} INFO - Done. Returned value was: None
。
请注意 (PythonOperator)
在此循环上方成功捕获,{python_operator}
是线路上的噪音。
我期望 Successful
的输出为 sc_sts = 'Succesful'
我很欣赏见解。我还在学习中。
删除中断,您将在第一个 If
之后退出循环你不理解的情绪break
似乎是正确的。 break
中断 最内层的封闭 for 或 while 循环。它用于提前停止循环的执行。我会通读有关流量控制的文档:https://docs.python.org/3/tutorial/controlflow.html
with open(path, "r") as f:
started = None
for line in f.readlines():
line.strip()
if started is None:
if operator in line:
#Do something here
else:
if "success" in line:
#Do something here
started = None
else:
pass
这对我有用。删除中断并更改 elif 语句。
for i, line in enumerate(lines):
if (fl_sts in line):
end_line = lines[i]
success_status = fl_sts
break
elif operator_type == op_py:
for line in lines:
if (sc_crt_py in line):
end_line = lines[-1]
success_status = sc_sts
elif operator_type == op_eml:
for line in lines:
if (sc_crt_eml in line):
end_line = lines[-1]
success_status = sc_sts
elif operator_type == op_s3:
for line in lines:
if (sc_crt_s3 in line):
end_line = lines[-1]
success_status = sc_sts
elif operator_type == op_bsh:
for line in lines:
if (sc_crt_bsh in line):
end_line = lines[-1]
success_status = sc_sts
elif operator_type == op_snw:
for line in lines:
if (sc_crt_snw in line):
end_line = lines[-1]
success_status = sc_sts
elif operator_type == op_snw_sns:
for line in lines:
if (sc_crt_snw_sns in line):
end_line = lines[-1]
success_status = sc_sts
else:
success_status = sc_sts_rn
end_line = 'None'