arunmenon
arunmenon

Reputation: 65

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:

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.

Upvotes: 0

Views: 4825

Answers (1)

Pear
Pear

Reputation: 351

  1. Store it in variable
  2. Convert to string
  3. Search for did not validate in the converted string
  4. the string lies in 127th position
  5. search for { in the string as it is the starting after the error
  6. takeout the thing after the position
  7. convert it to any form you want
  8. ALSO: print the variable a for confirmation
  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)

Upvotes: 2

Related Questions