使用zlib和cPickle将字典压缩/解压缩为文件

时间:2012-09-03 06:08:42

标签: python zlib pickle

我正在使用python向zlib压缩和cPickled字典写入文件。它似乎工作,但是,我无法弄清楚如何重新读取文件。

我包含以下代码,其中包括我尝试过的几项内容(以及相关的错误消息)。我无处可去。

import sys
import cPickle as pickle
import zlib

testDict = { 'entry1':1.0, 'entry2':2.0 }

with open('test.gz', 'wb') as fp:
  fp.write(zlib.compress(pickle.dumps(testDict, pickle.HIGHEST_PROTOCOL),9))

attempt = 0

try:
  attempt += 1
  with open('test.gz', 'rb') as fp:
    step1 = zlib.decompress(fp)
    successDict = pickle.load(step1)
except Exception, e:
  print "Failed attempt:", attempt, e

try:
  attempt += 1
  with open('test.gz', 'rb').read() as fp:
    step1 = zlib.decompress(fp)
    successDict = pickle.load(step1)
except Exception, e:
  print "Failed attempt:", attempt, e

try:
  attempt += 1
  with open('test.gz', 'rb') as fp:
    step1 = zlib.decompress(fp.read())
    successDict = pickle.load(step1)
except Exception, e:
  print "Failed attempt:", attempt, e

try:
  attempt += 1
  with open('test.gz', 'rb') as fp:
    d = zlib.decompressobj()
    step1 = fp.read()
    step2 = d.decompress(step1)
    step3 = pickle.load(step2)
except Exception ,e:
  print "Failed attempt:", attempt, e

try:
  attempt += 1
  with open('test.gz', 'rb') as fp:
    d = zlib.decompressobj()
    step1 = fp.read()
    step2 = d.decompress(step1)
    step3 = pickle.load(step2)
except Exception ,e:
  print "Failed attempt:", attempt, e

我收到以下错误:

Failed attempt: 1 must be string or read-only buffer, not file
Failed attempt: 2 __exit__
Failed attempt: 3 argument must have 'read' and 'readline' attributes
Failed attempt: 4 argument must have 'read' and 'readline' attributes
Failed attempt: 5 argument must have 'read' and 'readline' attributes

希望这只是一些显而易见的(对其他人)修复,我只是缺少。谢谢你的帮助!

2 个答案:

答案 0 :(得分:8)

您在尝试3-5时遇到的错误是因为您正在使用pickle.load而不是pickle.loads。前者需要一个类似文件的对象,而不是你从解压缩调用中得到的字节串。

这将有效:

with open('test.gz', 'rb') as fp:
    data = zlib.decompress(fp.read())
    successDict = pickle.loads(data)

答案 1 :(得分:3)

根据pickle手册,你需要使用pickle.load*s* http://docs.python.org/release/2.5/lib/node317.html(显然删除'*')