简体   繁体   English

如何从 python 中的 object 中提取数据

[英]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.我将异常的内容转移到一个变量中,只需要弄清楚如何读取该变量。

  1. Store it in variable将其存储在variable
  2. Convert to string转换为string
  3. Search for did not validate in the converted string在转换后的字符串中搜索did not validate
  4. the string lies in 127 th position该字符串位于第127个 position
  5. search for { in the string as it is the starting after the error在字符串中搜索{因为它是错误之后的开始
  6. takeout the thing after the position取出position之后的东西
  7. convert it to any form you want将其转换为您想要的任何形式
  8. ALSO: print the variable a for confirmation还:打印变量a进行确认
  9. CODE:代码:
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.

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