Created
June 26, 2019 23:28
-
-
Save franccesco/d98087cb0b560dff103fba225eb2d8cb to your computer and use it in GitHub Desktop.
Saving a dictionary with Pickle
This file contains 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
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