简体   繁体   English

检查字典是否包含命令输出中的某个字符串

[英]Check if dictionary contains a certain string from command output

I'm new to learning python and pexpect and I'm trying to check if a dictionary contains a certain string from some command output but I'm having trouble.我是学习 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。 I can confirm this works.我可以确认这有效。

If I were to put all of this in a python script and check if the two values are equal (ignoring capitalization), it should pass the check.如果我将所有这些放在一个 python 脚本中并检查两个值是否相等(忽略大写),它应该通过检查。 However, when I try to check if the first string (the code from the list command) is in the dictionary, it outputs 'NOT EQUAL'但是,当我尝试检查第一个字符串(list 命令中的代码)是否在字典中时,它输出“NOT EQUAL”

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

# convert dictionary to string
secondString = str(license_type)

if firstString.lower() in secondString.lower()
    print("EQUAL")
else:
    print("NOT EQUAL")

I'm not allowed to change the key names/capitalization of what's in the dictionary.我不允许更改字典中内容的键名/大写。 I tried converting the dictionary to a string and also used .lower() to convert both strings to lowercase.我尝试将字典转换为字符串,并使用 .lower() 将两个字符串转换为小写。 I'm not sure what I did wrong and would appreciate any help or references.我不确定我做错了什么,希望得到任何帮助或参考。

Instead of converting the dictionary to a string just access the value of the key license and convert it to a lowercase string and check if the firstString and it are equal.无需将字典转换为字符串,只需访问密钥license的值并将其转换为小写字符串并检查firstString是否相等。 I am assuming that the regex you are using is correct.我假设您使用的正则表达式是正确的。

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

if firstString.lower() == license_type.get("license").lower():
    print("EQUAL")
else:
    print("NOT EQUAL")

I tested it out in my iPython shell so it should work:我在 iPython shell 中对其进行了测试,因此它应该可以工作:

In [3]: "4KG-LV".lower() == license_type.get("license").lower()
Out[3]: True

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

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