简体   繁体   English

如何使用 python 正则表达式从大量显示的 output 中提取特定消息?

[英]How to extract particlar message from a vast displayed output using python regular expression?

  1. Firstly in the code, i would like to know How can i add a for loop for CH (1-11) instead of writing for every number首先在代码中,我想知道如何为 CH (1-11) 添加 for 循环而不是为每个数字编写
  2. Also how to extract SUCCESS and FAILED message from the output (reference) For example i want the output as还有如何从 output 中提取 SUCCESS 和 FAILED 消息(参考)例如我想要 output 作为

CH1: Failed CH1:失败

CH2: SUCCESS CH2:成功

CH3: Failed CH3:失败

: so on : 很快

I want to use regular expression and not json for this.我想为此使用正则表达式而不是 json。

import pexpect
  def quick_test():   
    ch = pexpect.spawn('ssh to server')
    ch.logfile = sys.stdout
    ch.expect("Select channels")
    print ("\n########################################\n")
    ch.sendline("1")
    ch.expect("Enter ch to run:")
    ch.sendline("CH1,0")
    var1=ch.after
    print(var1)
    ch.expect("Enter Test:")
    var2=ch.before
    print(var2)
    ch.sendline("CH2,0")
    ch.expect("Enter Test:")
    var3=ch.before
    print(var3)
    ch.sendline("CH3,0")
    ch.expect("Enter Test:")
    var4=ch.before
    print(var4)
    ch.sendline("CH4,0")
    ch.expect("Enter Test:")
    var5=ch.before
    print(var5)
    ch.sendline("CH5,0")
    ch.expect("Enter Test:")
    var6=ch.before
    print(var6)
    ch.sendline("CH6,0")
    ch.expect("Enter Test:")
    var7=ch.before
    print(var7)
    ch.sendline("CH7,0")
    ch.expect("Enter Test:")
    var8=ch.before
    print(var8)
    ch.sendline("CH8,0")
    ch.expect("Enter Test:")
    var9=ch.before
    print(var9)
    ch.sendline("CH9,0")
    ch.expect("Enter Test:")
    var10=ch.before
    print(var10)
    ch.sendline("CH10,0")
    ch.expect("Enter Test:")
    var11=ch.before
    print(var11)
    ch.sendline("CH11,0")

if __name__ == '__main__':
    quick_test()

output: output:

    output
    ###########################################
    There are plenty of output displayed in which these below lines are included and 
    not in the given order and are displayed randomly.

CH1,0 Result: FAILED
CH2,0 Result: SUCCESS
CH3,0 Result: FAILED
CH4,0 Result: SUCCESS
CH5,0 Result: SUCCESS
CH6,0 Result: SUCCESS
CH7,0 Result: FAILED
CH8,0 Result: SUCCESS
CH9,0 Result: FAILED
CH10,0 Result: SUCCESS
CH11,0 Result: FAILED

1. Firstly in the code, i would like to know How can i add a for loop for CH (1-11) instead of writing for every number 1.首先在代码中,我想知道如何为 CH (1-11) 添加 for 循环而不是为每个数字编写

Your basic repeatable unit, starting on "CH2", is:从“CH2”开始的基本可重复单位是:

ch.sendline("CH2,0")
ch.expect("Enter Test:")
var3=ch.before
print(var3)

Instead of using named variables, we will use a single variable holding a list data structure to hold n values:我们将不使用命名变量,而是使用一个包含列表数据结构的变量来保存 n 个值:

vars = []

We use a for loop to iterate up to 11:我们使用 for 循环迭代到 11:

for i in range(11):
    ch.sendline("CH{},0".format(i+1))
    ch.expect("Enter Test:")
    vars[i]=ch.before
    print(vars[i])

The first variable is handled differently, so we deal with it outside the loop:第一个变量的处理方式不同,因此我们在循环外处理它:

vars = []
ch.expect("Enter ch to run:")
ch.sendline("CH1,0")
var1s[0]=ch.after
print(var1)
for i in range(1, 11):
    ch.sendline("CH{},0".format(i+1))
    ch.expect("Enter Test:")
    vars[i]=ch.before
    print(vars[i])

This should print the same text to the display, and your values should still be stored in the vars list.这应该将相同的文本打印到显示器上,并且您的值仍应存储在vars列表中。 For example, what used to be in var8 will now be in vars[7] (since arrays are zero-indexed).例如,以前在var8中的内容现在将在vars[7]中(因为 arrays 是零索引)。

2. Also how to extract SUCCESS and FAILED message from the output (reference) 2. 还有如何从 output 中提取 SUCCESS 和 FAILED 消息(参考)

Use a RegEx, such as this pattern:使用正则表达式,例如此模式:

^(CH\d{1,}),0 Result: (SUCCESS|FAILED)$

You will get the desired strings in two positional capture groups.您将在两个位置捕获组中获得所需的字符串。

You can match against each line in the output (assuming this output is stored somewhere, such as read in from a file, and not simply printed to the display) by again using a for loop.您可以再次使用 for 循环匹配 output 中的每一行(假设此 output 存储在某处,例如从文件中读入,而不是简单地打印到显示器)。

Make use of Python's re module:使用 Python 的re模块:

pattern = r"^(CH\d{1,}),0 Result: (SUCCESS|FAILED)$" # r-string
sampleOutputLine = "CH1,0 Result: FAILED"
m = re.match(pattern, sampleOutputLine)

print(m.groups())

Output: Output:

('CH1', 'FAILED')

You can then format the groups as desired, for example as:然后,您可以根据需要格式化组,例如:

formattedOutputLine = "{}: ".format(m.groups[0])
if m.groups[1] === "SUCCESS":
    formattedOutputLine += m.groups[1]
else:
    formattedOutputLine += m.groups[1].lower()

Assuming the output lines are stored as a list of strings in the variable output , where each string is a line:假设 output 行作为字符串列表存储在变量output中,其中每个字符串是一行:

pattern = r"^(CH\d{1,}),0 Result: (SUCCESS|FAILED)$" # r-string

formattedOutput = []
for line in output:
    m = re.match(pattern, line) # consider compiling the pattern beforehand if your output is large, for performance
    formattedOutputLine = "{}: ".format(m.groups[0])
if m.groups[1] === "SUCCESS":
    formattedOutputLine += m.groups[1]
else:
    formattedOutputLine += m.groups[1].lower()
    
    formattedOutput.append(formattedOutputLine)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM