【发布时间】:2021-12-03 14:04:48
【问题描述】:
这是我的 json 文件,其中包含嵌套字典中的示例数据,如下所示
data = {
'school': {
'name': 'abc',
'class': {
'firstclass': {'teacher': 'b', 'students': '10'},
'secondclass': {'teacher': 'c', 'students': '25'},
}, 'city': 'x'},
'college': {
'name': 'def',
'class': {
'firstclass': {'teacher': 'd', 'students': '9'},
'secondclass': {'teacher': 'e', 'students': '65'},
}, 'city': 'y'},
'university': {
'name': 'ghi',
'class': {
'firstclass': {'teacher': 'f', 'students': '55'},
'secondclass': {'teacher': 'g', 'students': '22'},
}, 'city': 'z'}
}
我的输出应该是这样的:
'teacher':'b','students':'10'
'teacher':'d','students':'9'
'teacher':'f','students':'55'
这是我在 for 循环中的代码:
for id in data:
for j in data[id]:
print(j ,"=" , data[id][j])
我没有超出预期的输出。 如何获得如上的准确输出。
【问题讨论】:
-
您正在显式打印
"=",这甚至不在您的预期输出中?! -
另外,请发布可以逐字复制的数据结构。你的
data不是有效的dict -
for j in data[id]['class']
标签: json python-3.x pandas dictionary