473,408 Members | 2,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

pickle

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
4 2330
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Michael Hohn | last post by:
Hi, under python 2.2, the pickle/unpickle sequence incorrectly restores a larger data structure I have. Under Python 2.3, these structures now give an explicit exception from...
0
by: Mike P. | last post by:
Hi all, I'm working on a simulation (can be considered a game) in Python where I want to be able to dump the simulation state to a file and be able to load it up later. I have used the standard...
6
by: Jim Lewis | last post by:
Pickling an instance of a class, gives "can't pickle instancemethod objects". What does this mean? How do I find the class method creating the problem?
10
by: crystalattice | last post by:
I'm creating an RPG for experience and practice. I've finished a character creation module and I'm trying to figure out how to get the file I/O to work. I've read through the python newsgroup...
5
by: Chris | last post by:
Why can pickle serialize references to functions, but not methods? Pickling a function serializes the function name, but pickling a staticmethod, classmethod, or instancemethod generates an...
3
by: fizilla | last post by:
Hello all! I have the following weird problem and since I am new to Python I somehow cannot figure out an elegant solution. The problem reduces to the following question: How to pickle a...
2
by: Michele Simionato | last post by:
Can somebody explain what's happening with the following script? $ echo example.py import pickle class Example(object): def __init__(self, obj, registry): self._obj = obj self._registry =...
2
by: Nagu | last post by:
I am trying to save a dictionary of size 65000X50 to a local file and I get the memory error problem. How do I go about resolving this? Is there way to partition the pickle object and combine...
0
by: Nagu | last post by:
I am trying to save a dictionary of size 65000X50 to a local file and I get the memory error problem. How do I go about resolving this? Is there way to partition the pickle object and combine...
1
by: IceMan85 | last post by:
Hi to all, I have spent the whole morning trying, with no success to pickle an object that I have created. The error that I get is : Can't pickle 'SRE_Match' object: <_sre.SRE_Match object at...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.