import json,time
# save data to json file
def store(data):
with open('data.json', 'w') as fw:
# 将字典转化为字符串
# json_str = json.dumps(data)
# fw.write(json_str)
# 上面两句等同于下面这句
json.dump(data,fw)
# load json data from file
def load():
with open('data.json','r') as f:
data = json.load(f)
return data
if __name__ == "__main__":
json_data = '{"login":[{"username":"aa","password":"001"},{"username":"bb","password":"002"}],"register":[{"username":"cc","password":"003"},{"username":"dd","password":"004"}]}'
# 函数是将json格式数据转换为字典
data = json.loads(json_data)
store(data)
data = load()
print(data)
上一篇
Python 编程规范 —— TODO 注释(结合 PyCharm)
2024-05-08
下一篇
py2读取文件指定编码
2024-05-08