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.
So say I send some command called "list" to my terminal, it outputs details of a certain product:
->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)':
{'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.
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. 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. However, when I try to check if the first string (the code from the list command) is in the dictionary, it outputs '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. 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. 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:
In [3]: "4KG-LV".lower() == license_type.get("license").lower()
Out[3]: True