{
"xyz" : {
"SERVER": "xxx@localhost",
"WEBHCAT_SERVER": "http://qws.qs.i5.internal.cloudapp.net:50111/templeton/v1/",
},
"SNSPARK": {
"PROJECT_BASE": "/home/databrik/newdp/projects",
"SCRIPT_BASE": "/home/databrik/newdp/scripts",
"DATA_FILE_BASE": "/home/databrik"
},
"LOCAL": {
"PROJECT_BASE": "/home/pica/Installation/databricks/platform/projects",
"SCRIPT_BASE": "/home/pica/Installation/databricks/platform/scripts",
"DATA_FILE_BASE": "/home/pica",
"CB_URL_BASE": "http://11.222.222.111:3017/api/v1"
},
"DEFAULT_BACKEND": "LOCAL",
"public": {
"USE_LDAP": false
}
}
I have above json file for which i want to change the value for all keys under "LOCAL"key that is through a python code my json file should change to the following :
{
"xyz" : {
"SERVER": "xxx@localhost",
"WEBHCAT_SERVER": "http://qws.qs.i5.internal.cloudapp.net:50111/templeton/v1/",
},
"SNSPARK": {
"PROJECT_BASE": "/home/databrik/newdp/projects",
"SCRIPT_BASE": "/home/databrik/newdp/scripts",
"DATA_FILE_BASE": "/home/databrik"
},
"LOCAL": {
"PROJECT_BASE": "/home/louis/databricks/platform/projects",
"SCRIPT_BASE": "/home/louis/databricks/platform/scripts",
"DATA_FILE_BASE": "/home/louis",
"CB_URL_BASE": "http://22.222.222.22:3017/api/v1"
},
"DEFAULT_BACKEND": "LOCAL",
"public": {
"USE_LDAP": false
}
}
Can anyone help in this please ?
答案 0 :(得分:2)
Use the json
module and str.replace
.
import json
d = """{
"xyz" : {
"SERVER": "xxx@localhost",
"WEBHCAT_SERVER": "http://qws.qs.i5.internal.cloudapp.net:50111/templeton/v1/"
},
"SNSPARK": {
"PROJECT_BASE": "/home/databrik/newdp/projects",
"SCRIPT_BASE": "/home/databrik/newdp/scripts",
"DATA_FILE_BASE": "/home/databrik"
},
"LOCAL": {
"PROJECT_BASE": "/home/pica/Installation/databricks/platform/projects",
"SCRIPT_BASE": "/home/pica/Installation/databricks/platform/scripts",
"DATA_FILE_BASE": "/home/pica",
"CB_URL_BASE": "http://11.222.222.111:3017/api/v1"
},
"DEFAULT_BACKEND": "LOCAL",
"public": {
"USE_LDAP": false
}
}"""
d = json.loads(d)
for k in d["LOCAL"]:
d["LOCAL"][k] = d["LOCAL"][k].replace("pica/Installation", "louis")
print(json.dumps(d, indent=2))
Output:
{
"SNSPARK": {
"PROJECT_BASE": "/home/databrik/newdp/projects",
"SCRIPT_BASE": "/home/databrik/newdp/scripts",
"DATA_FILE_BASE": "/home/databrik"
},
"DEFAULT_BACKEND": "LOCAL",
"public": {
"USE_LDAP": false
},
"LOCAL": {
"PROJECT_BASE": "/home/pica/louis/databricks/platform/projects",
"SCRIPT_BASE": "/home/pica/louis/databricks/platform/scripts",
"DATA_FILE_BASE": "/home/pica",
"CB_URL_BASE": "http://11.222.222.111:3017/api/v1"
},
"xyz": {
"WEBHCAT_SERVER": "http://qws.qs.i5.internal.cloudapp.net:50111/templeton/v1/",
"SERVER": "xxx@localhost"
}
}