python - 从嵌套字典中提取数据,TypeError : string indices must be integers

标签 python json dictionary

我收到以下错误

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/

相关文章:

javascript - AttributeError at/accounts/regist_save/'User' 对象没有属性 'user'

python - 'Decimal' 类型的对象不是 JSON 可序列化的

c# - 选择一个好的字典键

python - python中的字符串匹配

python - 如何对没有http请求的python脚本使用flask-socketio

javascript - 从对象中删除空值或空值

javascript - 从 json 对象中提取精确的字符串

python - 如果列表具有相同的值,则使用 zip 将两个列表合并为键值对

c++ - 通过 C++ 套接字发送 std::map<int, std::map<std::string, double>> (linux)

python - 是否可以创建 "def"的列表?