[英]How to extract data from an object in python
I am getting an exception which am transferring to a variable.我收到一个正在转移到变量的异常。 The output of the variable is given below:变量的output如下:
schema.SchemaError("Key 'policy' error:\nOr({'name': And(<class 'str'>, <built-in function len>), 'age': And(<class 'int'>, <function <lambda> at 0x000001C964E92DC8>), Optional('gender'): And(<class 'str'>, Use(<method 'lower' of 'str' objects>), <function <lambda> at 0x000001C964E92E58>)}) did not validate {'name': 'Sue', 'age': 28, 'gnder': 'Squid'}\nWrong key 'gnder' in {'name': 'Sue', 'age': 28, 'gnder': 'Squid'}")
Please let me know how I can get the contents of this variable, I could try to convert this into a string but then it becomes very cumbersome to extract the contents.请让我知道如何获取此变量的内容,我可以尝试将其转换为字符串,但提取内容变得非常麻烦。 I specifically want the contents after 'did not validate' part.我特别想要“未验证”部分之后的内容。
EDIT - Not looking to resolve the error, error was expected.编辑 - 不希望解决错误,预计会出现错误。 I transferred the contents of the exception to a variable, just need to figure out how to read the variable.我将异常的内容转移到一个变量中,只需要弄清楚如何读取该变量。
variable
将其存储在variable
中string
转换为string
did not validate
in the converted string在转换后的字符串中搜索did not validate
127
th position该字符串位于第127
个 position{
in the string as it is the starting after the error在字符串中搜索{
因为它是错误之后的开始position
取出position
之后的东西a
for confirmation还:打印变量a
进行确认a = schema.SchemaError("Key 'policy' error:\nOr({'name': And(<class 'str'>, <built-in function len>), 'age': And(<class 'int'>, <function <lambda> at 0x000001C964E92DC8>), Optional('gender'): And(<class 'str'>, Use(<method 'lower' of 'str' objects>), <function <lambda> at 0x000001C964E92E58>)}) did not validate {'name': 'Sue', 'age': 28, 'gnder': 'Squid'}\nWrong key 'gnder' in {'name': 'Sue', 'age': 28, 'gnder': 'Squid'}")
a = str(a)
b = 127 #every error starts from here
a = a[b:len(a)]
c = a.find("{")
a = a[c:len(a)]
print(a)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.