Issue
I'm trying to one by one, go through each json object and subtract 1 from each value, but I'm not sure where to start...resources on this specific thing are scarce online so I came here...
{
"12345678910":32,
"10987654321":21 // after one loop will change to 31 and 20
}
json_file=json.load(file)
for object in json_file:
# subtract 1 from value
Solution
You're almost there. Inside the for
loop you just need to
json_file[object] -= 1
and then:
json.dump(file, json_file)
Note though that you shouldn't use object
as a variable name as it shadows the inbuilt class. Below I've changed your code to use key
instead.
Full code:
with open('file.json', 'r') as file:
json_file=json.load(file)
for key in json_file:
# subtract 1 from value
json_file[key] -= 1
with open('file.json', 'w') as file:
json.dump(json_file, file)
Answered By - Nick
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.