Skip to content

Instantly share code, notes, and snippets.

@KyeRussell
Created July 3, 2011 01:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KyeRussell/1061880 to your computer and use it in GitHub Desktop.
Save KyeRussell/1061880 to your computer and use it in GitHub Desktop.
Python Pickler Example
import pickle
import random
# Opening a file handler.
fh = open('obj.txt', 'w')
# Create a new object.
eggs = {'green eggs': 'ham',
'sam' : 'i am'}
# Lets see what's there...
print(eggs)
# Creating a new Pickler.
pickler = pickle.Pickler(fh)
# Dump eggs object to file.
pickler.dump(eggs)
import pickle
# Opening our object dumpfile.
fh = open('obj.txt', 'r')
# Creating the unpickler object.
unpk = pickle.Unpickler(fh)
# Re-load the eggs object from our dumpfile.
eggs = unpk.load()
# Here's our object, safe and sound.
print(eggs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment