- 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
- Also how to extract SUCCESS and FAILED message from the output (reference)
For example i want the output as
CH1 : Failed
CH2: SUCCESS
CH3: Failed
: so on
I want to use regular expression and not json for this.
import pexpectdef quick_test(): ch = pexpect.spawn('ssh to server')ch.logfile = sys.stdoutch.expect("Select channels")print ("\n########################################\n")ch.sendline("1")ch.expect("Enter ch to run:")ch.sendline("CH1,0")var1=ch.afterprint(var1)ch.expect("Enter Test:")var2=ch.beforeprint(var2)ch.sendline("CH2,0")ch.expect("Enter Test:")var3=ch.beforeprint(var3)ch.sendline("CH3,0")ch.expect("Enter Test:")var4=ch.beforeprint(var4)ch.sendline("CH4,0")ch.expect("Enter Test:")var5=ch.beforeprint(var5)ch.sendline("CH5,0")ch.expect("Enter Test:")var6=ch.beforeprint(var6)ch.sendline("CH6,0")ch.expect("Enter Test:")var7=ch.beforeprint(var7)ch.sendline("CH7,0")ch.expect("Enter Test:")var8=ch.beforeprint(var8)ch.sendline("CH8,0")ch.expect("Enter Test:")var9=ch.beforeprint(var9)ch.sendline("CH9,0")ch.expect("Enter Test:")var10=ch.beforeprint(var10)ch.sendline("CH10,0")ch.expect("Enter Test:")var11=ch.beforeprint(var11)ch.sendline("CH11,0")if __name__ == '__main__':quick_test()
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
Your basic repeatable unit, starting on "CH2", is:
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:
vars = []
We use a for loop to iterate up to 11:
for i in range(11):ch.sendline("CH{},0".format(i+1))ch.expect("Enter Test:")vars[i]=ch.beforeprint(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.beforeprint(vars[i])
This should print the same text to the display, and your values should still be stored in the vars
list. For example, what used to be in var8
will now be in vars[7]
(since arrays are zero-indexed).
2. Also how to extract SUCCESS and FAILED message from the output (reference)
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.
Make use of Python's re
module:
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:
('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:
pattern = r"^(CH\d{1,}),0 Result: (SUCCESS|FAILED)$" # r-stringformattedOutput = []
for line in output:m = re.match(pattern, line) # consider compiling the pattern beforehand if your output is large, for performanceformattedOutputLine = "{}: ".format(m.groups[0])
if m.groups[1] === "SUCCESS":formattedOutputLine += m.groups[1]
else:formattedOutputLine += m.groups[1].lower()formattedOutput.append(formattedOutputLine)