且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在python中将数据结构持久化到文件的最简单方法?

更新时间:2022-03-24 08:52:53

使用 pickle 模块.

import pickle
d = { "abc" : [1, 2, 3], "qwerty" : [4,5,6] }
afile = open(r'C:d.pkl', 'wb')
pickle.dump(d, afile)
afile.close()

#reload object from file
file2 = open(r'C:d.pkl', 'rb')
new_d = pickle.load(file2)
file2.close()

#print dictionary object loaded from file
print new_d