Skip to content

Instantly share code, notes, and snippets.

@gwpantazes
Last active August 17, 2018 22:05
Show Gist options
  • Save gwpantazes/0343b997c5ab601f70e772606afe9d16 to your computer and use it in GitHub Desktop.
Save gwpantazes/0343b997c5ab601f70e772606afe9d16 to your computer and use it in GitHub Desktop.
Python Pickle Example
#!/usr/bin/env python3
# How to use the Python pickle module to store arbitrary python data as a file.
# Cleaned up from the demo at: https://pythontips.com/2013/08/02/what-is-pickle-in-python/
import pickle
# Create something to store
a = ['test value','test value 2','test value 3']
# And something to compare against later
b = []
# Dump a into a pickle file as bytes
with open("testPickleFile", 'wb') as f:
pickle.dump(a, f)
# Load from the previous pickle file as bytes
with open("testPickleFile", 'rb') as f:
b = pickle.load(f)
# Now we can compare the original data vs the loaded pickle data
print(b) # ['test value','test value 2','test value 3']
print(a==b) # true
# And it's equivalent!
# That's storing and loading data via pickle!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment