python 将dictionary 转化成json并且写入和读取json文件

import json

#将数据存入json文件 name:[gender,age,password]
user_dict = {"tracy": ["female",16,"123456"],
"bella": ["female",17,"password"],
"colin": ["male",18,"colin"]
}
#写入json文件
with open(‘userinfo.json‘, ‘w‘) as json_file:
  json.dump(user_dict, json_file)
#读取json文件并打印
with open(‘userinfo.json‘, ‘r‘) as json_file:
  load_dict = json.load(json_file)
  print(load_dict["tracy"])

相关推荐