Created
August 4, 2019 08:10
-
-
Save rudrathegreat/eacd72b72bf3ed91afa8d0bc3ffeedd9 to your computer and use it in GitHub Desktop.
Pickling a dictionary and then unpickling it
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 | |
from random import randint | |
from datetime import datetime | |
# LINES 6-18 ARE USED TO PICKLE SOMETHING TO DISK | |
present = datetime.now() | |
data_dict = { | |
'voltage': randint(1,5), | |
'current': randint(1,5), | |
'resistance': randint(1,3), | |
'timestamp': str(present.strftime('%d/%M/%Y %H:%m:%s')) | |
} | |
with open('sample.pickle', 'wb') as file: | |
pickle.dump(data_dict, file) | |
print('Saved') | |
# LINES 21 ONWARDS ARE USED TO UNPICKLE SOMETHING | |
with open('sample.pickle', 'rb') as file: | |
data_dict = pickle.load(file) | |
print(data_dict) | |
print(data_dict['voltage']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment