pickle

Discussion in 'Python' started by Shi Mu, Oct 25, 2005.

  1. Shi Mu

    Shi Mu Guest

    I got a sample code and tested it but really can not understand the
    use of pickle and dump:

    >>> import pickle
    >>> f = open("try.txt", "w")
    >>> pickle.dump(3.14, f)
    >>> pickle.dump([1,2,3,4], f)
    >>> f.close()
     
    Shi Mu, Oct 25, 2005
    #1
    1. Advertising

  2. Shi Mu

    marduk Guest

    On Mon, 2005-10-24 at 23:55 -0700, Shi Mu wrote:
    > I got a sample code and tested it but really can not understand the
    > use of pickle and dump:
    >
    > >>> import pickle
    > >>> f = open("try.txt", "w")
    > >>> pickle.dump(3.14, f)
    > >>> pickle.dump([1,2,3,4], f)
    > >>> f.close()


    The pickle module "serializes" python objects. You can "dump" a python
    object that can later be loaded:

    >>> x = complex(2,3.5)
    >>> f = open("cnumber.pickle", "w")
    >>> import pickle
    >>> pickle.dump(x, f)
    >>> f.close()
    >>> y = pickle.load(file("cnumber.pickle", "r"))
    >>> y

    (2+3.5j)
     
    marduk, Oct 25, 2005
    #2
    1. Advertising

  3. Shi Mu

    Shi Mu Guest

    what does the following code mean?

    y = pickle.load(file("cnumber.pickle", "r"))

    also, I can not understand "f" in pickle.dump(x, f)

    On 10/25/05, marduk <> wrote:
    > On Mon, 2005-10-24 at 23:55 -0700, Shi Mu wrote:
    > > I got a sample code and tested it but really can not understand the
    > > use of pickle and dump:
    > >
    > > >>> import pickle
    > > >>> f = open("try.txt", "w")
    > > >>> pickle.dump(3.14, f)
    > > >>> pickle.dump([1,2,3,4], f)
    > > >>> f.close()

    >
    > The pickle module "serializes" python objects. You can "dump" a python
    > object that can later be loaded:
    >
    > >>> x = complex(2,3.5)
    > >>> f = open("cnumber.pickle", "w")
    > >>> import pickle
    > >>> pickle.dump(x, f)
    > >>> f.close()
    > >>> y = pickle.load(file("cnumber.pickle", "r"))
    > >>> y

    > (2+3.5j)
    >
    >
    >
     
    Shi Mu, Oct 25, 2005
    #3
  4. Shi Mu wrote:

    > what does the following code mean?
    >
    > y = pickle.load(file("cnumber.pickle", "r"))


    open the file "cnumber.pickle" for reading, pass the file handle to
    the pickle.load function, and store the result in the "y" variable.

    > also, I can not understand "f" in pickle.dump(x, f)


    the second argument to pickle is a file handle, opened for writing.
    pickle.dump will save the "pickled data" to that file.

    </F>
     
    Fredrik Lundh, Oct 25, 2005
    #4
  5. Shi Mu

    Steve Holden Guest

    Shi Mu wrote:
    > what does the following code mean?
    >
    > y = pickle.load(file("cnumber.pickle", "r"))
    >

    Take it by parts:

    file("cnumber.pickle", "r")

    returns a file object as a result of opening the "cnumber.pickle" file,
    which is presumably a pickle someone wrote earlier.

    So

    pickle.load(file("cnumber.pickle", "r"))

    returns the first object stored in that pickle file.


    > also, I can not understand "f" in pickle.dump(x, f)
    >

    The beginning of the docs on pickle usage says:

    """dump( obj, file[, protocol[, bin]])

    Write a pickled representation of obj to the open file object file."""

    Isn't this reasopnably self-explanatory?

    regards
    Steve

    >[...]


    --
    Steve Holden +44 150 684 7255 +1 800 494 3119
    Holden Web LLC www.holdenweb.com
    PyCon TX 2006 www.python.org/pycon/
     
    Steve Holden, Oct 25, 2005
    #5
    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. Ben Finney

    Securing 'pickle'

    Ben Finney, Jul 11, 2003, in forum: Python
    Replies:
    17
    Views:
    599
    Paul Rubin
    Jul 11, 2003
  2. Aki Niimura

    freeze utility and pickle

    Aki Niimura, Aug 21, 2003, in forum: Python
    Replies:
    1
    Views:
    551
    =?ISO-8859-1?Q?Gerhard_H=E4ring?=
    Aug 21, 2003
  3. Bram Stolk
    Replies:
    2
    Views:
    361
    Peter Otten
    Sep 23, 2003
  4. a pickle's pickle

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

Share This Page