Differences between revisions 3 and 4
Revision 3 as of 2004-04-22 19:58:25
Size: 1030
Editor: dsl254-010-130
Comment: Found an official pickle example.
Revision 4 as of 2004-04-22 20:06:00
Size: 1431
Editor: dsl254-010-130
Comment: Simple example. Pickle a dictionary, and retrieve it.
Deletions are marked like this. Additions are marked like this.
Line 9: Line 9:
== Pickle Example ==

{{{
#!python
# Save a dictionary into a pickle file.
import pickle

favorite_color = { "lion": "yellow", "kitty": "red" }

pickle.dump( favorite_color, open( "save.p", "w" ) )
}}}

{{{
#!python
# Load the dictionary back from the pickle file.
import pickle

favorite_color = pickle.load( open( "save.p" ) )
# 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.]
Line 14: Line 37:

== Code Example ==

''Need some simple code here, when it's figured out.''

''Should pickle a dictionary, list, and string. Simple simple simple.''

{{{
#!python
# Relevant Python code
class Something():
  pass

x=Something()
print x
}}}

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

  • (none yet)

UsingPickle (last edited 2019-12-15 07:34:41 by FrancesHocutt)

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