本文介绍了读取json文件并获取输出值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用python获取以下json文件的输出
I want to fetch the output of below json file using python
Json文件
{
"Name": [
{
"name": "John",
"Avg": "55.7"
},
{
"name": "Rose",
"Avg": "71.23"
},
{
"name": "Lola",
"Avg": "78.93"
},
{
"name": "Harry",
"Avg": "95.5"
}
]
}
当我寻找哈利时,我想获得该人的平均分数即我需要以下或类似格式的输出
I want to get the average marks of the person, when I look for harryi.e. I need output in below or similar format
Harry = 95.5
这是我的代码
import json
json_file = open('test.json') //the above contents are stored in the json
data = json.load(json_file)
do = data['Name'][0]
op1 = do['Name']
if op1 is 'Harry':
print do['Avg']
但是当我运行时,出现错误IOError: [Errno 63] File name too long
.
But when I run I get error IOError: [Errno 63] File name too long
.
推荐答案
您可以执行以下简单操作
You can do something simple like this
import json
file = open('data.json')
json_data = json.load(file)
stud_list = json_data['Name']
y = {}
for var in stud_list:
x = {var['name']: var['Avg']}
y = dict(list(x.items()) + list(y.items()))
print(y)
它以字典格式给出输出
{'哈利':'95 .5','萝拉':'78 .93','罗斯':'71 .23','约翰':'55 .7'}
{'Harry': '95.5', 'Lola': '78.93', 'Rose': '71.23', 'John': '55.7'}
这篇关于读取json文件并获取输出值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!