Created
November 2, 2021 22:45
-
-
Save nick3499/468af495219d0be1ffd575e1d81e4878 to your computer and use it in GitHub Desktop.
Pickle Test: dict(), pickle, unpickle, serialize, deserialize, with open()
This file contains hidden or 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 | |
example_dict = {'one': '1', 'two': '2', 'three': '3', 'four': '4', 'five': '5', 'six': '6', 'seven': '7'} | |
with open('dict_pickle', 'wb') as pickle_out: | |
pickle.dump(example_dict, pickle_out) | |
print('\x1b[32mPrint pickled or serialized dictionary:\x1b[0m\n') | |
with open('/home/nick/Desktop/dict_pickle', 'rb') as file_out: | |
print(f'{file_out.read()}\n') | |
with open('dict_pickle', 'rb') as pickle_in: | |
example_dict_load = pickle.load(pickle_in) | |
print('\x1b[32mPrint unpickled or deserialized dictionary:\x1b[0m\n') | |
print(f'{example_dict_load}\n') | |
print('\x1b[32mPrint values from dictionary:\x1b[0m\n') | |
print(f"\x1b[33mexample_dict_load['one']\x1b[0m --> {example_dict_load['one']}") | |
print(f"\x1b[33mexample_dict_load['two']\x1b[0m --> {example_dict_load['two']}") | |
print(f"\x1b[33mexample_dict_load['three']\x1b[0m --> {example_dict_load['three']}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Print pickled or serialized dictionary:
Print unpickled or deserialized dictionary:
Print values from dictionary: