Python在一个字符串中进行多重if搜索

2024-06-08 13:01:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我´我是一个没有编程经验的网络工程师,最近用的是python,但每天都在做一些小的改进

我需要一些帮助来获取IF语句中的多个匹配项,例如:

if  "access-class 30" in output and "exec-timeout 5 5" in output:
    print ('###### ACL VTY OK!!! ######')

是否可以在一个字符串中检查多个关键字? 谢谢你的时间


Tags: andin网络outputifaccess编程timeout
2条回答

对生成器表达式使用all函数:

data = ["access-class 30", "exec-timeout 5 5"]
if all(s in output for s in data):
    print('###### ACL VTY OK!!! ######')

是的,这是可能的

可以使用正则表达式(Regex)

import re
li = [] # List of all the keywords
for l in li
  for m in re.finditer(l,output)
     if m !=None:
       print 'match found'

相关问题 更多 >