【发布时间】:2023-04-06 08:04:01
【问题描述】:
我有一本像这样的字典
elements = {
"pipelineDescriptionList": [
{
"pipelineId": "df-09694461I15A2UPH0RZT",
"name": "EQUIPMENT_DEV",
"fields": [
{
"key": "@lastActivationTime",
"stringValue": "2020-12-09T05:30:21"
},
{
"key": "@nextRunTime",
"stringValue": "2021-04-12T00:10:00"
},
{
"key": "@creationTime",
"stringValue": "2020-12-09T05:26:06"
},
{
"key": "@sphere",
"stringValue": "PIPELINE"
},
{
"key": "@healthStatusUpdatedTime",
"stringValue": "2021-04-11T00:15:22"
},
{
"key": "@scheduledStartTime",
"stringValue": "2020-12-09T00:10:00"
},
{
"key": "@healthStatus",
"stringValue": "HEALTHY"
},
{
"key": "@latestRunTime",
"stringValue": "2021-04-11T00:10:00"
},
{
"key": "@version",
"stringValue": "1"
},
{
"key": "name",
"stringValue": "EQUIPMENT_DEV"
},
{
"key": "@id",
"stringValue": "df-09694461I15A2UPH0RZT"
},
{
"key": "@pipelineState",
"stringValue": "SCHEDULED"
},
{
"key": "uniqueId",
"stringValue": "96AEF597-33FD-420A-8D39-D328EBE0EC1C"
},
{
"key": "@scheduledPeriod",
"stringValue": "1 day"
},
{
"key": "@firstActivationTime",
"stringValue": "2020-12-09T05:30:21"
}
],
"tags": []
}
]
}
我需要访问键@pipelineState、@nextRunTime、@lastActivationTime 的字符串值。 我目前正在通过
访问它们elements['pipelineDescriptionList'][0]['fields'][0]['stringValue']
elements['pipelineDescriptionList'][0]['fields'][1]['stringValue']
elements['pipelineDescriptionList'][0]['fields'][10]['stringValue']
但是我的管道字典不断变化,相同键的索引值对于不同的管道会有所不同。 例如:
elements = {
"pipelineDescriptionList": [
{
"pipelineId": "df-0440555DDFN1Z0N4UOR",
"name": "API_TOKEN_DEV",
"fields": [
{
"key": "@creationTime",
"stringValue": "2020-09-04T02:11:20"
},
{
"key": "@cancelActive",
"stringValue": "true"
},
{
"key": "@startTimestamp",
"stringValue": "2020-09-08T14:31:29"
},
{
"key": "pipelineCreator",
"stringValue": "AROAU2XMJTS2T67LQAZTF:kiranv@bizcloudexperts.com"
},
{
"key": "@version",
"stringValue": "2"
},
{
"key": "@id",
"stringValue": "df-0440555DDFN1Z0N4UOR"
},
{
"key": "@lastActivationTime",
"stringValue": "2020-09-08T14:31:29"
},
{
"key": "@nextRunTime",
"stringValue": "2021-04-12T02:11:00"
},
{
"key": "@lastDeactivationRequestTime",
"stringValue": "2020-09-05T02:09:29"
},
{
"key": "@sphere",
"stringValue": "PIPELINE"
},
{
"key": "@healthStatusUpdatedTime",
"stringValue": "2021-04-11T02:16:25"
},
{
"key": "@scheduledStartTime",
"stringValue": "2020-09-04T02:11:00"
},
{
"key": "@healthStatus",
"stringValue": "HEALTHY"
},
{
"key": "@latestRunTime",
"stringValue": "2021-04-11T02:11:00"
},
{
"key": "name",
"stringValue": "API_TOKEN_DEV"
},
{
"key": "@lastDeactivationTime",
"stringValue": "2020-09-05T02:11:29"
},
{
"key": "@pipelineState",
"stringValue": "SCHEDULED"
},
{
"key": "@firstActivationTime",
"stringValue": "2020-09-04T02:13:33"
}
]}
]
}
如何在不使用索引的情况下访问所需键的字符串值?有没有办法可以直接使用键名来获取 stringValues ?
【问题讨论】:
-
你可以使用
.get() -
您能详细说明一下吗?
标签: python python-3.x list dictionary nested-lists