我是python的新手。这是一个嵌套的JSON
文件。我在Jupyter Notebook
上使用python。
我想创建一个循环,以便提取unitsWattSeconds
和billAmount
。我无法从h12
迭代到其他小时。
with open('C:\\work\\greenturn\\selvam.json') as f:
json_data = json.load(f)
for hour in json_data['2018-09-03']['Hourly']:
print(json_data['2018-09-03']['Hourly']['h12'])
在这里,我不明白如何创建从h12
到h13
等的迭代循环。
{
"2018-09-03" : {
"ExtraSummary" : {
"phantomUnitsKwh" : 855.312
},
"Hourly" : {
"h12" : {
"billAmount" : 0,
"createdAt" : 1535957349481,
"fromTimestamp" : 1535957349481,
"hour" : "h12",
"toTimestamp" : 1535957349481,
"unitsWattSeconds" : 211044826,
"updatedAt" : 1535957349481
},
"h13" : {
"billAmount" : 0,
"createdAt" : 1535960342711,
"fromTimestamp" : 1535960342711,
"hour" : "h13",
"toTimestamp" : 1535960342711,
"unitsWattSeconds" : 282814657,
"updatedAt" : 1535960342711
},
"h14" : {
"billAmount" : 0,
"createdAt" : 1535963945252,
"fromTimestamp" : 1535963945252,
"hour" : "h14",
"toTimestamp" : 1535963945252,
"unitsWattSeconds" : 270476208,
"updatedAt" : 1535963945252
},
"h15" : {
"billAmount" : 0,
"createdAt" : 1535967544013,
"fromTimestamp" : 1535967544013,
"hour" : "h15",
"toTimestamp" : 1535967544013,
"unitsWattSeconds" : 232294125,
"updatedAt" : 1535967544013
},
"h16" : {
"billAmount" : 0,
"createdAt" : 1535971142799,
"fromTimestamp" : 1535971142799,
"hour" : "h16",
"toTimestamp" : 1535971142799,
"unitsWattSeconds" : 263890609,
"updatedAt" : 1535971142799
},
"h17" : {
"billAmount" : 0,
"createdAt" : 1535974744637,
"fromTimestamp" : 1535974744637,
"hour" : "h17",
"toTimestamp" : 1535974744637,
"unitsWattSeconds" : 219126193,
"updatedAt" : 1535974744637
},
"h18" : {
"billAmount" : 0,
"createdAt" : 1535978342751,
"fromTimestamp" : 1535978342751,
"hour" : "h18",
"toTimestamp" : 1535978342751,
"unitsWattSeconds" : 233109086,
"updatedAt" : 1535978342751
},
"h19" : {
"billAmount" : 0,
"createdAt" : 1535981943846,
"fromTimestamp" : 1535981943846,
"hour" : "h19",
"toTimestamp" : 1535981943846,
"unitsWattSeconds" : 220127313,
"updatedAt" : 1535981943846
},
"h20" : {
"billAmount" : 0,
"createdAt" : 1535985544790,
"fromTimestamp" : 1535985544790,
"hour" : "h20",
"toTimestamp" : 1535985544790,
"unitsWattSeconds" : 240706357,
"updatedAt" : 1535985544790
},
"h21" : {
"billAmount" : 0,
"createdAt" : 1535989143882,
"fromTimestamp" : 1535989143882,
"hour" : "h21",
"toTimestamp" : 1535989143882,
"unitsWattSeconds" : 197464734,
"updatedAt" : 1535989143882
},
"h22" : {
"billAmount" : 0,
"createdAt" : 1535992744209,
"fromTimestamp" : 1535992744209,
"hour" : "h22",
"toTimestamp" : 1535992744209,
"unitsWattSeconds" : 226102413,
"updatedAt" : 1535992744209
},
"h23" : {
"billAmount" : 0,
"createdAt" : 1535996342258,
"fromTimestamp" : 1535996342258,
"hour" : "h23",
"toTimestamp" : 1535996342258,
"unitsWattSeconds" : 248254455,
"updatedAt" : 1535996342258
}
},
"Summary" : {
"billAmount" : 0,
"createdAt" : 1535957349561,
"date" : "2018-09-03",
"toTimestamp" : 1535957349561,
"unitsKwh" : 788.304046,
"updatedAt" : 1535957349561
}
}
答案 0 :(得分:1)
使用.items()
例如:
for hour, value in json_data['2018-09-03']['Hourly'].items():
print(hour, value)
输出:
('h14', {'billAmount': 0, 'hour': 'h14', 'fromTimestamp': 1535963945252L, 'toTimestamp': 1535963945252L, 'updatedAt': 1535963945252L, 'unitsWattSeconds': 270476208, 'createdAt': 1535963945252L})
('h15', {'billAmount': 0, 'hour': 'h15', 'fromTimestamp': 1535967544013L, 'toTimestamp': 1535967544013L, 'updatedAt': 1535967544013L, 'unitsWattSeconds': 232294125, 'createdAt': 1535967544013L})
('h23', {'billAmount': 0, 'hour': 'h23', 'fromTimestamp': 1535996342258L, 'toTimestamp': 1535996342258L, 'updatedAt': 1535996342258L, 'unitsWattSeconds': 248254455, 'createdAt': 1535996342258L})
('h17', {'billAmount': 0, 'hour': 'h17', 'fromTimestamp': 1535974744637L, 'toTimestamp': 1535974744637L, 'updatedAt': 1535974744637L, 'unitsWattSeconds': 219126193, 'createdAt': 1535974744637L})
('h18', {'billAmount': 0, 'hour': 'h18', 'fromTimestamp': 1535978342751L, 'toTimestamp': 1535978342751L, 'updatedAt': 1535978342751L, 'unitsWattSeconds': 233109086, 'createdAt': 1535978342751L})
('h19', {'billAmount': 0, 'hour': 'h19', 'fromTimestamp': 1535981943846L, 'toTimestamp': 1535981943846L, 'updatedAt': 1535981943846L, 'unitsWattSeconds': 220127313, 'createdAt': 1535981943846L})
('h12', {'billAmount': 0, 'hour': 'h12', 'fromTimestamp': 1535957349481L, 'toTimestamp': 1535957349481L, 'updatedAt': 1535957349481L, 'unitsWattSeconds': 211044826, 'createdAt': 1535957349481L})
('h13', {'billAmount': 0, 'hour': 'h13', 'fromTimestamp': 1535960342711L, 'toTimestamp': 1535960342711L, 'updatedAt': 1535960342711L, 'unitsWattSeconds': 282814657, 'createdAt': 1535960342711L})
('h21', {'billAmount': 0, 'hour': 'h21', 'fromTimestamp': 1535989143882L, 'toTimestamp': 1535989143882L, 'updatedAt': 1535989143882L, 'unitsWattSeconds': 197464734, 'createdAt': 1535989143882L})
('h20', {'billAmount': 0, 'hour': 'h20', 'fromTimestamp': 1535985544790L, 'toTimestamp': 1535985544790L, 'updatedAt': 1535985544790L, 'unitsWattSeconds': 240706357, 'createdAt': 1535985544790L})
('h16', {'billAmount': 0, 'hour': 'h16', 'fromTimestamp': 1535971142799L, 'toTimestamp': 1535971142799L, 'updatedAt': 1535971142799L, 'unitsWattSeconds': 263890609, 'createdAt': 1535971142799L})
('h22', {'billAmount': 0, 'hour': 'h22', 'fromTimestamp': 1535992744209L, 'toTimestamp': 1535992744209L, 'updatedAt': 1535992744209L, 'unitsWattSeconds': 226102413, 'createdAt': 1535992744209L})