Zipped and pickle

C

Carl Banks

How do I implement best to use pickle that way that the file is zipped?

Briefly:

s = cPickle.dumps(obj)
z = zipfile.Zipfile("filename.zip","w",zipfile.ZIP_DEFLATED)
z.writestr("arcname.pkl",s)


Carl Banks
 
T

Thomas Lehmann

Briefly:
s = cPickle.dumps(obj)
z = zipfile.Zipfile("filename.zip","w",zipfile.ZIP_DEFLATED)
z.writestr("arcname.pkl",s)

Thank you very much. I have not been aware that pickle can also do the
job without a file!
Here's the complete scenario for writing and reading the data...

APPENDIX:

import pickle
import zipfile

def test1():
print("test1...")

# create data
data = {}
data["first name" ] = "Thomas"
data["second name"] = "Lehmann"
data["hobbies" ] = ["programming python"]
print (data)

# pickle data
pickleString = pickle.dumps(data)
# save string to zip under a name
file = zipfile.ZipFile("ZippedPickle.zip", "w",
zipfile.ZIP_DEFLATED)
file.writestr("some data", pickleString)
file.close()

def test2():
print("test2...")
file = zipfile.ZipFile("ZippedPickle.zip", "r",
zipfile.ZIP_DEFLATED)
# reading zipped string store under a name
pickleString = file.read("some data")
# unpickle string to original data
data = pickle.loads(pickleString)
print (data)
file.close()

if __name__ == "__main__":
test1()
test2()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top