输入:
{
"ConcurrentAsyncGetReportInstances": {
"Max": 200,
"Remaining": 200
},
"ConcurrentSyncReportRuns": {
"Max": 20,
"Remaining": 20
},
必需输出:
{"Wraper":{
"Max1":200,
"Remaining1": 200,
"Max2":20,
"Remaining2": 20,
},
使用的代码:
r=["Max"+str(i) for i in range(0)]
with open('hai.txt') as f:
for line in f:
if "Max" in line:
print line
上面代码输出的是:
"Max": 200,
"Max": 20,
请建议任何更好的代码,以便按照我所知道的所有可能的方式提供输出
答案 0 :(得分:1)
data.json
{
"ConcurrentAsyncGetReportInstances": {
"Max": 200,
"Remaining": 200
},
"ConcurrentSyncReportRuns": {
"Max": 20,
"Remaining": 20
}
}
read_json.py
import json
with open('data.json') as data_file:
data = json.load(data_file)
output = {"Wraper": {}}
i = 1
for key, sub_data in data.iteritems():
for key_sub, value in sub_data.iteritems():
output["Wraper"] [key_sub + str(i)] = value
i += 1
print output
输出:
{'Wraper': {u'Max2': 20, u'Max1': 200, u'Remaining2': 20, u'Remaining1': 200}}