Revision 4 as of 2004-04-22 20:06:00

Clear message

Using Pickle

Pickle Example

   1 # Save a dictionary into a pickle file.
   2 import pickle
   3 
   4 favorite_color = { "lion": "yellow", "kitty": "red" }
   5 
   6 pickle.dump( favorite_color, open( "save.p", "w" ) )

   1 # Load the dictionary back from the pickle file.
   2 import pickle
   3 
   4 favorite_color = pickle.load( open( "save.p" ) )
   5 # favorite_color is now { "lion": "yellow", "kitty": "red" }

For a more complex example, see [http://www.python.org/doc/current/lib/pickle-example.html the official Pickle example,] and for API details, see the [http://www.python.org/doc/current/lib/node64.html official Pickle use documentation.]

Flying Pickle Alert!

Pickle files can be hacked. If you receive a raw pickle file over the network, don't trust it! It could have malicious code in it, that would run arbitrary python when you try to de-pickle it.

However, if you are doing your own pickle writing and reading, you're safe. (Provided no one else has access to the pickle file, of course.)

Contributors

LionKimbro

Discussion

Unable to edit the page? See the FrontPage for instructions.