Skip to content

Instantly share code, notes, and snippets.

@rudrathegreat
Created August 4, 2019 08:10
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 rudrathegreat/eacd72b72bf3ed91afa8d0bc3ffeedd9 to your computer and use it in GitHub Desktop.
Save rudrathegreat/eacd72b72bf3ed91afa8d0bc3ffeedd9 to your computer and use it in GitHub Desktop.
Pickling a dictionary and then unpickling it
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