By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,818 Members | 1,313 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,818 IT Pros & Developers. It's quick & easy.

pickle

P: n/a
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()

Oct 25 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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)
Oct 25 '05 #2

P: n/a
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 <us****@marduk.letterboxes.org> 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)

Oct 25 '05 #3

P: n/a
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>

Oct 25 '05 #4

P: n/a
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/

Oct 25 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.