Created
July 3, 2011 01:35
-
-
Save KyeRussell/1061880 to your computer and use it in GitHub Desktop.
Python Pickler Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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