Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Zipped and pickle

Reply
Thread Tools

Zipped and pickle

 
 
Thomas Lehmann
Guest
Posts: n/a
 
      09-16-2009
How do I implement best to use pickle that way that the file is zipped?
 
Reply With Quote
 
 
 
 
Carl Banks
Guest
Posts: n/a
 
      09-16-2009
On Sep 16, 4:40*am, Thomas Lehmann <Iris-und-Thomas-Lehm...@T-
Online.de> wrote:
> 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_DEF LATED)
z.writestr("arcname.pkl",s)


Carl Banks
 
Reply With Quote
 
 
 
 
Thomas Lehmann
Guest
Posts: n/a
 
      09-17-2009
> Briefly:
>
> s = cPickle.dumps(obj)
> z = zipfile.Zipfile("filename.zip","w",zipfile.ZIP_DEF LATED)
> 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()
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pickle error: can't pickle instancemethod objects Michele Simionato Python 2 05-23-2008 08:29 AM
zipped firefox Ed Mullen Firefox 5 09-21-2005 03:07 AM
a pickle's pickle temposs@gmail.com Python 4 08-02-2005 07:20 PM
Location of Fire Fox zipped archive Erronius Firefox 2 02-24-2005 05:50 PM
Problems opening Zipped files =?Utf-8?B?Z3JhcGh0ZXI=?= Microsoft Certification 0 05-27-2004 08:21 AM



Advertisments