Skip to content

Instantly share code, notes, and snippets.

@slee0
Created December 9, 2012 02:51
Show Gist options
  • Select an option

  • Save slee0/4243105 to your computer and use it in GitHub Desktop.

Select an option

Save slee0/4243105 to your computer and use it in GitHub Desktop.
[python] Simple pickle serialization example
# 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