pickle

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Shi Mu

    #1

    pickle

    I got a sample code and tested it but really can not understand the
    use of pickle and dump:
    [color=blue][color=green][color=darkred]
    >>> import pickle
    >>> f = open("try.txt", "w")
    >>> pickle.dump(3.1 4, f)
    >>> pickle.dump([1,2,3,4], f)
    >>> f.close()[/color][/color][/color]
  • marduk

    #2
    Re: pickle

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

    The pickle module "serializes " python objects. You can "dump" a python
    object that can later be loaded:
    [color=blue][color=green][color=darkred]
    >>> x = complex(2,3.5)
    >>> f = open("cnumber.p ickle", "w")
    >>> import pickle
    >>> pickle.dump(x, f)
    >>> f.close()
    >>> y = pickle.load(fil e("cnumber.pick le", "r"))
    >>> y[/color][/color][/color]
    (2+3.5j)


    Comment

    • Shi Mu

      #3
      Re: pickle

      what does the following code mean?

      y = pickle.load(fil e("cnumber.pick le", "r"))

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

      On 10/25/05, marduk <usenet@marduk. letterboxes.org > wrote:[color=blue]
      > On Mon, 2005-10-24 at 23:55 -0700, Shi Mu wrote:[color=green]
      > > I got a sample code and tested it but really can not understand the
      > > use of pickle and dump:
      > >[color=darkred]
      > > >>> import pickle
      > > >>> f = open("try.txt", "w")
      > > >>> pickle.dump(3.1 4, f)
      > > >>> pickle.dump([1,2,3,4], f)
      > > >>> f.close()[/color][/color]
      >
      > The pickle module "serializes " python objects. You can "dump" a python
      > object that can later be loaded:
      >[color=green][color=darkred]
      > >>> x = complex(2,3.5)
      > >>> f = open("cnumber.p ickle", "w")
      > >>> import pickle
      > >>> pickle.dump(x, f)
      > >>> f.close()
      > >>> y = pickle.load(fil e("cnumber.pick le", "r"))
      > >>> y[/color][/color]
      > (2+3.5j)
      >
      >
      >[/color]

      Comment

      • Fredrik Lundh

        #4
        Re: pickle

        Shi Mu wrote:
        [color=blue]
        > what does the following code mean?
        >
        > y = pickle.load(fil e("cnumber.pick le", "r"))[/color]

        open the file "cnumber.pickle " for reading, pass the file handle to
        the pickle.load function, and store the result in the "y" variable.
        [color=blue]
        > also, I can not understand "f" in pickle.dump(x, f)[/color]

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

        </F>



        Comment

        • Steve Holden

          #5
          Re: pickle

          Shi Mu wrote:[color=blue]
          > what does the following code mean?
          >
          > y = pickle.load(fil e("cnumber.pick le", "r"))
          >[/color]
          Take it by parts:

          file("cnumber.p ickle", "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(fil e("cnumber.pick le", "r"))

          returns the first object stored in that pickle file.

          [color=blue]
          > also, I can not understand "f" in pickle.dump(x, f)
          >[/color]
          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
          [color=blue]
          >[...][/color]

          --
          Steve Holden +44 150 684 7255 +1 800 494 3119
          Holden Web LLC www.holdenweb.com
          PyCon TX 2006 www.python.org/pycon/

          Comment

          Working...