python_在pickling的时候压缩

 代码:


import cPickle,gzip
def save(filename,*objects):
    fil1 = gzip.open(filename,'wb')
    for obj in objects:
        cPickle.dump(obj,fil1,protocol = 2)
        fil1.close()
def load(filename):
    fil1 = gzip.open(filename,'rb')
    while True:
        try:
            yield cPickle.load(fil1)
        except EOFError:
            break
    fil1.close()
    
    
data1 = ['abc',12,23]    #几个测试数据
data2 = {1:'aaa',"b":'dad'}
data3 = (1,2,4)
data = list([data1,data2,data3])
save('data.zip',data)

iter = load('data.zip')
for item in iter:
    for data in item:
        print data

结果:

['abc', 12, 23]
{1: 'aaa', 'b': 'dad'}
(1, 2, 4)


你可能感兴趣的:(python_在pickling的时候压缩)