Well, that's a pretty ugly design, you should use json to store data in an external file, this way it's possible to load it and rewrite it. Consider this:
data.json
{
"person": {
"name": "Jhon Dow",
"address": "London",
"age": 26,
"isMarried": true,
"brothersNames": [
"Kim",
"David",
"Ron"
]
},
"animal": {
"type": "Lion",
"name": "Simba",
"age": 10
}
}
Now let's use this code:
import json
with open('data.json', 'r') as f:
data = json.load(f)
data['person']['name'] = 'JSON!'
with open('data.json', 'w') as f:
json.dump(data, f, indent=2)
Let's take a look at the file now:
{
"person": {
"isMarried": true,
"age": 26,
"name": "JSON!",
"brothersNames": [
"Kim",
"David",
"Ron"
],
"address": "London"
},
"animal": {
"age": 10,
"type": "Lion",
"name": "Simba"
}
}
Our modification is there. The order of the keys is different, if you want to preserve the order you can change the loading part like this:
from collections import OrderedDict
import json
with open('data.json', 'r') as f:
data = json.load(f, object_pairs_hook=OrderedDict)
If you really want to use your data structure as it is now instead, use a regex (but that's a ugly). Check here.