How to Solve Automatic error keyerror:***‘

r  =  {'code': 200, 'body': {'code': 200, 'msg': 'request success', 'data': {'username': 'a***', 'mobile': '132**51', 'companyType': 6, 'token': 'ey**RI', 'companyName': '** Company', 'nickname': 'ab**'}}}
body = r['body']
print(body)     # {'code': 200, 'msg': 'request success', 'data': {'username': 'a***', 'mobile': '132**51', 'companyType': 6, 'token': 'ey**RI', 'companyName': '**Company', 'nickname': 'ab**'}}
# Initialize the database object
conn = init_db("db_1")
# Search Results
res_db = conn.fetchone("select mobile from hy_user where username = 'abc'")
print("Database query result is: %s"%res_db)
# Validate the results
user_mobile = body['mobile']
assert user_mobile == res_db['mobile']

Keyerror: ‘mobile’

Because mobile is in the data dictionary of body, change to user_ Mobile = body [‘data ‘] [‘mobile’]


Read More: