Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > pickle

Reply
Thread Tools

pickle

 
 
Shi Mu
Guest
Posts: n/a
 
      10-25-2005
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()

 
Reply With Quote
 
 
 
 
marduk
Guest
Posts: n/a
 
      10-25-2005
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)


 
Reply With Quote
 
 
 
 
Shi Mu
Guest
Posts: n/a
 
      10-25-2005
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 <(E-Mail Removed)> 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)
>
>
>

 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      10-25-2005
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>



 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      10-25-2005
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/

 
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
a pickle's pickle temposs@gmail.com Python 4 08-02-2005 07:20 PM
PIX pickle John Caruso Cisco 2 12-13-2003 08:10 AM
freeze utility and pickle Aki Niimura Python 1 08-21-2003 03:00 PM
Securing 'pickle' Ben Finney Python 17 07-11-2003 08:30 PM



Advertisments