这是我的代码:

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']的下一个子级中,它又像插槽:1,插槽:2,插槽3一样开始。我希望它从上一个子级的最后一个数字继续。 像:插槽:4,插槽:5 ...

谢谢!

===============>>#1 票数:0

为什么不在整个代码中使用一个变量,而不是像下面这样的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) 

  ask by A770 translate from so

本文未有回复,本站智能推荐: