[英]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.