欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python實(shí)現(xiàn)在pickling的時候壓縮的方法

 更新時間:2014年09月25日 17:12:10   投稿:shichen2014  
這篇文章主要介紹了python實(shí)現(xiàn)在pickling的時候壓縮的方法,比較具有實(shí)用價值,需要的朋友可以參考下

本文實(shí)例講述了python實(shí)現(xiàn)在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]  #幾個測試數(shù)據(jù)
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

本文實(shí)例測試環(huán)境為Python2.7.6

程序運(yùn)行結(jié)果如下:

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

在程序運(yùn)行的同時會在同級目錄下生成data.zip文件。

希望本文所述對大家Python程序設(shè)計的學(xué)習(xí)有所幫助。

相關(guān)文章

最新評論