【问题标题】:How to get inner content of data in dictionary python如何在字典python中获取数据的内部内容
【发布时间】:2015-05-03 21:05:41
【问题描述】:

我是 python 新手,无法获取字典的内部内容。 我有一个“详细信息”作为字典,我想从“属性”中提取数据,所以我将有“时间”和“distinct_id”作为键

现在字典中的键是“事件”和“属性”,希望在字典中更深入

字典“详细信息”示例:

{"event":"User Profile - Page View","properties":{"time":1428969601,"distinct_id":"14cb3d99b195-0feaf7b1c-5c267370-c0000-14cb3d99b1a220"}} 

谢谢!

【问题讨论】:

  • d["properties"]["time"],你有什么不明白的?

标签: python dictionary key


【解决方案1】:

每个字典(dict的实例)都带有一个方法__getitem__(self, index),其中[]是一个语法糖。为了访问未嵌套的字典,您通常会使用 details['event']。

由于嵌套字典只是另一个字典,因此同样适用:details['properties']['time'] 将返回嵌套字典的“时间”。

如果你想让代码更好一点,你可以将第一个字典赋给一个变量

properties = details['properties']

并像在普通(非嵌套)字典中一样访问属性

print properties['time']
print properties['distinct_id']

【讨论】:

  • 谢谢@mpoledink,这正是我所需要的
【解决方案2】:

dict = {"event":"用户个人资料 - 页面浏览量","properties":{"time":1428969601,"distinct_id":"14cb3d99b195-0feaf7b1c-5c267370-c0000-14cb3d99b1a220"}}

dict['properties']['time']

【讨论】:

    猜你喜欢
    • 2011-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-29
    • 1970-01-01
    • 2021-04-28
    • 1970-01-01
    相关资源
    最近更新 更多