Skip to content

Instantly share code, notes, and snippets.

@franccesco
Created June 26, 2019 23:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save franccesco/d98087cb0b560dff103fba225eb2d8cb to your computer and use it in GitHub Desktop.
Save franccesco/d98087cb0b560dff103fba225eb2d8cb 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