如何用python读取json文件里指定的数据
JSON文件who.json内容如下:{"name":"mss","version":"1.0.0","description":"Policyissuesystem",...
JSON文件who.json内容如下:
{
"name": "mss",
"version": "1.0.0",
"description": "Policy issue system",
"main": "app.js",
"scripts": {
"dev": "pm2 reload processes.json --env dev",
"test": "pm2 reload processes.json --env test",
"pro": "pm2 reload processes.json",
"start": "node app.js "
},
"keywords": [
"service",
"mss",
"microService"
],
"author": "who@163.com ",
"license": "Apache-2.0",
"dependencies": {
"ab": "^2.0.0",
"ae": "^6.1.0",
"af": "^0.16.2",
"co-mysql": "^1.0.0",
"ca": "0.0.3",
"date-utils": "^1.2.21",
"exc": "^0.5.1",
"pgtool": "^7.8.1"
}
}
如果通过python读取这个json里"dependencies"里所包含的数据,并进行罗列,将数据转换成:"ab": "^2.0.0"转换成ab@^2.0.0这样的格式?
如有大神回答,请尽量详细,我是python初学者一个。 展开
{
"name": "mss",
"version": "1.0.0",
"description": "Policy issue system",
"main": "app.js",
"scripts": {
"dev": "pm2 reload processes.json --env dev",
"test": "pm2 reload processes.json --env test",
"pro": "pm2 reload processes.json",
"start": "node app.js "
},
"keywords": [
"service",
"mss",
"microService"
],
"author": "who@163.com ",
"license": "Apache-2.0",
"dependencies": {
"ab": "^2.0.0",
"ae": "^6.1.0",
"af": "^0.16.2",
"co-mysql": "^1.0.0",
"ca": "0.0.3",
"date-utils": "^1.2.21",
"exc": "^0.5.1",
"pgtool": "^7.8.1"
}
}
如果通过python读取这个json里"dependencies"里所包含的数据,并进行罗列,将数据转换成:"ab": "^2.0.0"转换成ab@^2.0.0这样的格式?
如有大神回答,请尽量详细,我是python初学者一个。 展开
1个回答
展开全部
import json
with open('who.json', 'r') as f:
data = json.load(f)
dependencies = data['dependencies']
for k, v in dependencies.iteritems():
print(f'{k}@{v}')
追问
大佬,请问如何对不同目录下的多个who.json进行读取呢?
追答
把所有目录名放到列表里,遍历列表重复上述操作。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询