python - 如何使用 JSON 中前一个值的最后一个数字继续新值?
问题描述
这是我的代码:
with open('res.json', 'r') as file1:
json_data = json.load(file1)
for g in json_data['groups']:
try:
for i, group in enumerate(g['resources']):
group['slot'] = i
except:
continue
with open("RES_Edited.JSON", 'w') as json_edited:
json.dump(json_data, json_edited, indent = 1)
它使每个插槽都像插槽:1,插槽:2,插槽:3,这很棒。但是在 g['resources'] 的下一个孩子中,它又像 slot: 1, slot: 2, slot 3 一样重新开始。我希望它会从前一个孩子的最后一个数字继续。喜欢:插槽:4,插槽:5 ...
谢谢!
解决方案
为什么不在整个代码中使用一个变量,而不是像下面这样的 for 循环:
with open('res.json', 'r') as file1:
json_data = json.load(file1)
i = 1
for g in json_data['groups']:
try:
for group in g['resources']:
group['slot'] = i
i += 1
except:
continue
with open("RES_Edited.JSON", 'w') as json_edited:
json.dump(json_data, json_edited, indent = 1)
推荐阅读
- ios - UITableView 没有在我的自定义单元格 UILabel 中正确显示数据和重用问题
- angular - 在 Angular 6 服务中获取当前路由参数的最佳方法是什么?
- asp.net - WAF is blocking ASP.NET website due to Scriptresource.axd
- node.js - Pro.find 不是猫鼬模型导出中的函数错误
- git - 需要帮助合并断开连接的 git 存储库
- css - React 本机 android 文本具有额外的底部填充
- jquery - 关闭身体点击动画
- node.js - LoopBack 将查找结果保存到变量中
- r - R : 从 rpart 预测到单行结果
- neo4j - Cypher 查询:获取符合条件的行详细信息