数据持久存储:pickle模块的基本使用

2017-01-01 10:46
import pickle

def save(obj):
    obj = {"a": 1, "b": 2, "c": 3}
    # 将 obj 持久化保存到文件 tmp.txt 中
    with open('data.pickle', 'w') as f:
        pickle.dump(obj, f)

def read():
    # 将 obj 持久化保存到文件 tmp.txt 中
    with open('data.pickle', 'r') as f:
        return pickle.load(f)