将字典写入文本文件,并取出

ab={'lee':21,'liu':18,'ma':20,'gao':22}

#字典写入文件
def write_dict(d):
with open('dict.txt','w') as file:
file.write(json.dumps(d))

#字典从文件中读出
def read_dict():
with open('dict.txt','r') as file:
st=file.read()
return json.loads(st)
posted @ 2020-08-20 16:57  风中的太阳  阅读(103)  评论(0)    收藏  举报