Json format corresponds to python inside the dictionary, can be easily saved through the JSON module processing, the following code is used to set a good example.
Save the JSON file
def save_js(jsf,path) :
with open(path,"w",encoding="utf-8") as f:
jsd = json.dumps(jsf)
f.write(jsd)
Copy the code
Reading json files
def load_js(path) :
with open(path,"r") as f:
jsd = f.read()
jsf = json.loads(jsd)
return jsf
Copy the code
Use the sample
import json
def save_js(jsf,path) :
with open(path,"w",encoding="utf-8") as f:
jsd = json.dumps(jsf)
f.write(jsd)
def load_js(path) :
with open(path,"r") as f:
jsd = f.read()
jsf = json.loads(jsd)
return jsf
test_dic = {"a":1."b":2}
save_js(test_dic,"test.json")
read_dic = load_js("test.json")
print(test_dic,read_dic)
Copy the code