python的序列化与反序列化(例子:dict保存成文件,文件读取成dict)

dict保存成文件(对象序列化)

d = dict(name='TSQ', age=18)

import pickle
with open("dict.file", "wb") as f:
    pickle.dump(d, f)

文件读取成dict(文件反序列化)

d = {}

import pickle
with open("dict.file", "rb") as f:
    d = pickle.load(f)

print(d)

print(d)的结果是

{'name': 'TSQ', 'age': 18}

 

posted @ 2018-10-11 12:44  fdu_taoshiqian  阅读(6770)  评论(0编辑  收藏  举报