Answer the question
In order to leave comments, you need to log in
How to update certain data in JSON?
There is a JSON file. In which the following data:
{"_TIMES": "1", "RunestoneRare": 0, "EssenceRare": 0, "DustRare": 0, "RunestoneEpic": 0, "EssenceEpic": 0, "DustEpic": 0, "RunestoneLegendary": 0, "EssenceLegendary": 0, "DustLegendary": 0, "Ruby": 0}
REWARDS
for the match of a word from JSON and want to add +1 to the value:import json
REWARD = 'RunestoneRare'
i = 0
REWARDS = {
"_TIMES":"1",
"RunestoneRare":0,
"EssenceRare":0,
"DustRare":0,
"RunestoneEpic":0,
"EssenceEpic":0,
"DustEpic":0,
"RunestoneLegendary":0,
"EssenceLegendary":0,
"DustLegendary":0,
"Ruby": 0
}
to_json = REWARDS
with open('sw_templates.json', 'w') as f:
f.write(json.dumps(to_json))
with open('sw_templates.json', 'r') as f:
if REWARD == 'RunestoneRare':
json_data = json.load(f)
print(json_data['RunestoneRare'])
json_data['RunestoneRare'] = i + 1
with open('sw_templates.json', 'w') as f:
f.write(json.dumps(json_data))
++i
, json_data['RunestoneRare'] += 1
, json_data['RunestoneRare'] = i + 1
- Does not help.
Answer the question
In order to leave comments, you need to log in
You don't have a loop here => you start the program again every time =>
REWARDS = {
"_TIMES":"1",
"RunestoneRare":0,
"EssenceRare":0,
"DustRare":0,
"RunestoneEpic":0,
"EssenceEpic":0,
"DustEpic":0,
"RunestoneLegendary" :0,
"EssenceLegendary":0,
"DustLegendary":0,
"Ruby": 0
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question