Zipped and pickle

Discussion in 'Python' started by Thomas Lehmann, Sep 16, 2009.

  1. How do I implement best to use pickle that way that the file is zipped?
    Thomas Lehmann, Sep 16, 2009
    #1
    1. Advertising

  2. Thomas Lehmann

    Carl Banks Guest

    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_DEFLATED)
    z.writestr("arcname.pkl",s)


    Carl Banks
    Carl Banks, Sep 16, 2009
    #2
    1. Advertising

  3. > 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()
    Thomas Lehmann, Sep 17, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Kelly

    Streaming ZIPPED files

    Mark Kelly, May 11, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    535
    Mark Kelly
    May 12, 2004
  2. Amelyan

    Download zipped files

    Amelyan, Mar 7, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    401
    Amelyan
    Mar 7, 2006
  3. NB
    Replies:
    5
    Views:
    4,159
  4. a pickle's pickle

    , Aug 2, 2005, in forum: Python
    Replies:
    4
    Views:
    382
  5. Michele Simionato
    Replies:
    2
    Views:
    1,871
    Michele Simionato
    May 23, 2008
Loading...

Share This Page