pickle

M

marduk

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:
(2+3.5j)
 
S

Shi Mu

what does the following code mean?

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

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

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:
(2+3.5j)
 
F

Fredrik Lundh

Shi said:
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>
 
S

Steve Holden

Shi said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top