我收到以下错误
TypeError: string indices must be integers
当我尝试打印嵌套字典中的一些数据时
{u'id': u'000123', u'payload': u"{'account_title': u’sam b’, 'phone_num': ‘1234567890’, 'security_pin': u'000000', 'remote_word': u’secret123’, 'email_address': ‘email@gmail.com’, 'password': u’password123’}”}
例如,假设上面的内容被分配给变量“account_info”
print(account_info['payload'])
<- 将打印从“payload”开始的所有内容
但是当我使用时:
print(account_info['payload']['email_address'])
我收到错误
TypeError: string indices must be integers
有什么想法吗?谢谢!
最佳答案
由于payload
的值是一个字符串,而不是字典,因此无法对其进行索引。删除嵌套字典周围的引号以使代码正常工作:
account_info = {u'id': u'000123', u'payload': {'account_title': u'sam b', 'phone_num': '1234567890', 'security_pin': u'000000', 'remote_word': u'secret123', 'email_address': 'email@gmail.com', 'password': u'password123'}}
print(account_info["payload"]["email_address"])
关于python - 从嵌套字典中提取数据,TypeError : string indices must be integers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57854333/