Estoy usando python para escribir en un archivo un diccionario comprimido y comprimido zlib. Parece que funciona, sin embargo, no puedo entender cómo leer el archivo.Uso de zlib ycPickle para comprimir/descomprimir un diccionario en archivos
Estoy incluyendo el siguiente código que incluye varias de las cosas que he intentado (y los mensajes de error asociados). No estoy llegando a ninguna parte.
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
que obtienen los siguientes errores:
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
Esperemos que esto es sólo algunas obvias (a otra persona) fijar que yo soy sólo faltan. ¡Gracias por tu ayuda!