[英]How to get specific data from a dictionary in python
所以我有一个json文件。 我在python中将其作为json文件加载,我尝试从字典中获取项目。 我可以访问'sha'作为'作者'并获取其所有组件,但我如何只访问'消息','名称'和'日期'?
我尝试使用此代码来访问“消息”,但是我收到了一个关键错误:
data = json.load(json_file)
print(str(type(data))) # class <dict>
for p in data['commits']:
print(p['message'])
我的文件看起来像这样:(它包含多个提交消息):
{
"commits":[
{
"sha":"exampleSHA",
"node_id":"exampleID",
"commit":{
"author":{
"name":"example",
"email":"example@example.se",
"date":"2015-09-07T22:06:51Z"
},
"committer":{
"name":"example",
"email":"example@example.se",
"date":"2015-09-07T22:06:51Z"
},
"message":"Added LaTex template and instructions",
"tree":{
"sha":"examplesha",
"url":"http://example.url"
},
"parents":[]
}}
]
}
我想从dict中获取所有这些“消息”。
结果是这样的:
AuthorName Message Date
example Added LaTex template and instructions 2013-07-08T20:16:51Z
example2 Message2 2015-09-07T22:06:51Z
.... .... .....
尝试这个 :
data = json.load(json_file)
print(str(type(data))) # class <dict>
for p in data['commits']:
print(p['commit']['message'])
message
字段嵌套在commit
字段中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.