Created
December 9, 2012 02:51
-
-
Save slee0/4243105 to your computer and use it in GitHub Desktop.
[python] Simple pickle serialization example
This file contains hidden or 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
| # Pickle Serialization, input and output | |
| try: | |
| import cpickle as pickle | |
| except ImportError: | |
| import pickle | |
| def create_pyp(fname_pickle): | |
| d0 = { | |
| 'test0': 0., | |
| 'test1': 1., | |
| } | |
| d1 = { | |
| 'test2': 2., | |
| 'test3': 3., | |
| } | |
| l = [d0, d1] | |
| with open(fname_pickle, 'wb') as fpick: | |
| for item in l: | |
| pickle.dump(item, fpick, protocol=pickle.HIGHEST_PROTOCOL) | |
| def unpick_pyp(fname_pickle): | |
| l = [] | |
| with open(fname_pickle, 'rb') as fpick: | |
| while True: | |
| try: | |
| l.append(pickle.load(fpick)) | |
| except EOFError: | |
| break | |
| for item in l: | |
| print item | |
| if __name__ == '__main__': | |
| fname_pickle = 'pickletest.pyp' | |
| create_pyp(fname_pickle) | |
| unpick_pyp(fname_pickle) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment