json可以序列化多次但是反序列化只能反序列化一次,shelve可以多次写入,存取也比较方便
1. shelve模块是一个简单的k,v将内存数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据格式
import shelved = shelve.open("mydbase")name = ["zhangsan","lisi","wangmz"] d["test"] = name #持久化列表d["age"] = 20 d.close()
import shelvedbase = shelve.open("mydbase")print(dbase.keys())print(dbase['test'])for i in dbase: print(i)d.close()
2. json模块
#json序列化import jsona = { "tony":22, 'james':34}with open('text.txt','w') as f: f.write(json.dumps(a))#json反序列化import jsonwith open('text.txt', 'r') as f: x = json.load(f) print(x["james"])
3. pickele