字典读写训练

字典写入json文件中:

import json
stu_mark={}
stu_mark["Name"]=input("Name=")
stu_mark["English"]=float(input("English="))
stu_mark["Music"]=float(input("Music="))
stu_mark["Chinese"]=float(input("Chinese="))
print(stu_mark)
# with open("stu_mark.json", mode='w', encoding='utf-8') as fp:
#     json.dump(stu_mark, fp,ensure_ascii=False,indent=2)
f=open("stu_mark.json",'w',encoding='utf-8')
json.dump(stu_mark,f,False,2)
f.close()

从json文件读入到字典中:

import json

# with open("stu_mark.json", mode='r', encoding='utf-8') as fp:
#     print(json.load(fp))
f=open("stu_mark.json",'r',encoding='utf-8')
stu_mark=json.load(f)
print(type(stu_mark))
print(stu_mark)
f.close()
原文地址:https://www.cnblogs.com/exesoft/p/13354273.html