我正在尝试从此嵌套字典的时间戳中创建列表,但遇到了困难。
{
'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowJob',
'builds': [
{
'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowRun',
'timestamp': 1571731200315
},
{
'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowRun',
'timestamp': 1571731020310
},
{
'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowRun',
'timestamp': 1571730995706
}, {
'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowRun',
'timestamp': 1571730900316
}, {
'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowRun',
'timestamp': 1571730600313
}
]
}
我尝试了以下操作,但是由于第二个值本身包含一个嵌套的字典,因此它不像我想的那样解析时间戳:
dictList=[]
for key, value in dict.items():
dictList.append([value])
print (dictList[1])
此尝试的输出如下:
[
[
{
'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowRun',
'timestamp': 1571731200315
},
{
'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowRun',
'timestamp': 1571731020310
},
{
'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowRun',
'timestamp': 1571730995706
},
{
'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowRun',
'timestamp': 1571730900316
},
{
'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowRun',
'timestamp': 1571730600313
}
]
]
我如何深入研究嵌套的内容并仅获取时间戳?
答案 0 :(得分:1)
dict = {'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowJob', 'builds': [{'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowRun', 'timestamp': 1571731200315}, {'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowRun', 'timestamp': 1571731020310}, {'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowRun', 'timestamp': 1571730995706}, {'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowRun', 'timestamp': 1571730900316}, {'_class': 'org.jenkinsci.plugins.workflow.job.WorkflowRun', 'timestamp': 1571730600313}]}
timestamps = []
for i in dict['builds']:
timestamps.append(i['timestamp'])
print(timestamps)
我不清楚您要的是什么,但这可以将所有时间戳记入列表。