Skip to content

Instantly share code, notes, and snippets.

@hardword
Forked from franccesco/pickle_me.py
Created July 3, 2019 13:34
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 hardword/3367ad232579df90724e204c7f8d944a to your computer and use it in GitHub Desktop.
Save hardword/3367ad232579df90724e204c7f8d944a to your computer and use it in GitHub Desktop.
Saving a dictionary with Pickle
import pickle
dictionary_a = {'string_1': 1, 'string_2': 2.2, 'string_3': True}
# Pickling (serializing) dictionary A into a file
with open('saved_object.pickle', 'wb') as filename:
pickle.dump(dictionary_a, filename)
# Unpickling (de-serializing) dictionary A into B
with open('saved_object.pickle', 'rb') as filename:
dictionary_b = pickle.load(filename)
# Dictionaries A and B remaing the same
print(f'Is dictionary_a == dictionary_b?: {dictionary_a == dictionary_b}')
# >>> Is dictionary_a == dictionary_b?: True
# Dictionaries have the same content
print(f'Dictionary A: {dictionary_a}')
print(f'Dictionary B: {dictionary_b}')
# >>> Dictionary A: {'string_1': 1, 'string_2': 2.2, 'string_3': True}
# >>> Dictionary B: {'string_1': 1, 'string_2': 2.2, 'string_3': True}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment