Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

Edit JSON item from file

$
0
0

I have the following code:

import jsonwith open("prefix.json", 'r+') as f:    data = json.load(f)def changeprefix(id, prefix):    global data    if not id in data:        json.dump(data, open("prefix.json", "w"), indent = 4)        data[id] = prefix    else:        # Code to edit existing itemchangeprefix("6", "?")

It opens a file named prefix.json and I want to be able to edit an existing item, for example:

{"item": "234"}

How can I change item to something else, for example to text here, then save the file?


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>