简体   繁体   English

pexpect.expect('(?<=Code:\\s).*(?=[\\r\\n])') 有时会从多行输出中得到意想不到的结果

[英]pexpect.expect('(?<=Code:\s).*(?=[\r\n])') sometimes got unexpected result from multiline output

I'm new to learning python and pexpect and I'm trying to check if a dictionary is equal to a certain string from some command output but for some reason the string comparison fails on the rare occassion but passes most of the time.我是学习 python 和 pexpect 的新手,我试图从某些命令输出中检查字典是否等于某个字符串,但由于某种原因,字符串比较在极少数情况下失败,但大部分时间都通过了。

So say I send some command called list to my terminal, it outputs details of a certain product:假设我向我的终端发送了一些名为list命令,它会输出某个产品的详细信息:

->list
Color: Maroon Red
Height: 150cm
Code: 4KG-LV
Material: Aluminum
Brand: Toyota 
#and hundreds of more lines...

I have a variable that represents a dictionary that outputs this when I print it using print(license_type) :我有一个代表字典的变量,当我使用print(license_type)打印它时输出它:

{'license': '4kg-lv'}

I basically want to check if the code 4KG-LV from the output of the list command is equal to the value 4kg-lv from the license in the dictionary.我基本上想检查list命令输出中的代码4KG-LV是否等于字典中许可证中的值4kg-lv

Doing this:这样做:

exp.sendline("list")
exp.expect('(?<=Code:\s).*(?=[\r\n])')
firstString = exp.after
print(firstString) 

parses the output of the list command using regex and gets/prints the value 4KG-LV .使用正则表达式解析 list 命令的输出并获取/打印值4KG-LV

If I were to put all of this in a python script and check if the two values are equal, it should pass the check as I printed those 2 strings out and they look identical on my terminal.如果我将所有这些放在一个 python 脚本中并检查两个值是否相等,它应该通过检查,因为我打印了这两个字符串并且它们在我的终端上看起来相同。 However, when I try to check if the first string (the code from the list command) is equal to the dictionary value, it will pass as EQUAL 95% of the time, but on the rare occasion it fails the comparison for some reason and I have no idea why.但是,当我尝试检查第一个字符串(list 命令中的代码)是否等于字典值时,它会在 95% 的情况下通过 EQUAL,但在极少数情况下它会由于某种原因导致比较失败,并且我不知道为什么。

license_type = {'license': '4kg-lv'}
exp.sendline("list")
exp.expect('(?<=Code:\s).*(?=[\r\n])')
codeString = exp.after

firstString = codeString.lower().strip(' \t\n\r')
secondString = licenseType.get("type").lower().strip(' \t\n\r')

if firstString == secondString
    print("EQUAL")
else:
    print("NOT EQUAL")

I have lots of commands and output above this section of code and I'm not sure if maybe it's reading an extra space or \\n sometimes when I run the script?我在这部分代码上方有很多命令和输出,我不确定它是否在读取额外的空间或\\n有时当我运行脚本时? It fails the comparison maybe like once every 30-40 runs.比较失败,可能每 30-40 次运行一次。 Without the .strip(' \\t\\n\\r') it automatically fails every time, but adding that seems to make it pass most of the time but I'm not sure what's causing it to fail on the rare occasion.如果没有.strip(' \\t\\n\\r')它每次都会自动失败,但是添加它似乎使它在大部分时间通过,但我不确定是什么导致它在极少数情况下失败。

Is it possible it's ignoring the spaces/tabs too late?是否有可能为时已晚忽略空格/制表符? Should I be adding strip.(' \\t\\n\\r') before .lower() ?我应该在.lower()之前添加strip.(' \\t\\n\\r') .lower()吗?

Seems like you should change看来你该换了

exp.expect('(?<=Code:\s).*(?=[\r\n])')

to

exp.expect('(?<=Code:\s)[^\r\n]*(?=[\r\n])')

otherwise, the greedy .* may possibly match cross newlines (depending on how fast the app outputs and how fast pexpect reads the data) , for example, "4KG-LV\\r\\nMaterial: Aluminum" .否则,贪婪的.*可能会匹配交叉换行符(取决于应用程序输出的速度和 pexpect 读取数据的速度) ,例如, "4KG-LV\\r\\nMaterial: Aluminum"


Example:例子:

A simple pexpect script foo.py :一个简单的 pexpect 脚本foo.py

import pexpect, sys

p = pexpect.spawnu('bash -c "echo : foo; ((RANDOM % 2)) && sleep .1; echo : bar" ')
p.expect('(?<=:\s).*(?=[\r\n])')
matched = p.after
p.expect(pexpect.EOF)

print('MATCHED: %s' % repr(matched) )

Try it:尝试一下:

$ for i in {1..10}; do python3 foo.py; done
MATCHED: 'foo\r\n: bar\r'
MATCHED: 'foo\r'
MATCHED: 'foo\r\n: bar\r'
MATCHED: 'foo\r'
MATCHED: 'foo\r'
MATCHED: 'foo\r'
MATCHED: 'foo\r'
MATCHED: 'foo\r\n: bar\r'
MATCHED: 'foo\r\n: bar\r'
MATCHED: 'foo\r\n: bar\r'

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

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