pickle.dump
序列化至文件
import pickle
class A:
def __init__(self, x, y):
self.x = x
self.y = y
a = A(2,3)
f = open('somefile','wb')
pickle.dump(a,f)
pickle.load
从文件反序列化
f = open('somefile','rb')
data = pickle.load(f)
print data.x, data.y # 2 3
pickle.dumps
序列化至字符串
s = pickle.dumps(a)
pickle.loads
从字符串反序列化
obj = pickle.loads(s)
print obj.x, obj.y # 2 3