持久化任何python的数据格式,对pickle的一个封装
import shelve # 准备数据 class Test(object): def __init__(self, n): self.n = n t1 = Test(123) t2 =Test(456) info = {"name": "Jimi", "age": 23} names = ["Alex", "Tom", "Jack"] # 写入 d = shelve.open("shelve") # 打开一个文件 d["names"] = names # 持久化列表 d["t1"] = t1 # 持久化类 d["t2"] = t2 d["info"] = info #持久化字典 d.close() # 读取 d = shelve.open("shelve") # 打开一个文件 print(d.get("info")) print(d.get("names")) print(d.get("t1")) d.close() """ {'age': 23, 'name': 'Jimi'} ['Alex', 'Tom', 'Jack'] <__main__.Test object at 0x00000000059577B8> """