- Notifications
You must be signed in to change notification settings - Fork 638
Expand file tree
/
Copy pathP60_PickleModule.py
More file actions
Latest commit
32 lines (25 loc) · 1.01 KB
/
Copy pathP60_PickleModule.py
File metadata and controls
32 lines (25 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Author: OMKAR PATHAK
# In this example we will see how to use pickle module for storing the data efficiently!
# The pickle module translates an in-memory Python object into a serialized byte stream—a string of bytes
# that can be written to any file-like object.
importpickle
defstoreData():
# initializing data to be stored in db
Omkar= {'key' : 'Omkar', 'name' : 'Omkar Pathak', 'age' : 21, 'pay' : 40000}
Jagdish= {'key' : 'Jagdish', 'name' : 'Jagdish Pathak', 'age' : 50, 'pay' : 50000}
# database
db= {}
db['Omkar'] =Omkar
db['Jagdish'] =Jagdish
dbfile=open('examplePickle', 'ab') # Its important to use binary mode
pickle.dump(db, dbfile) # source, destination
dbfile.close()
defloadData():
dbfile=open('examplePickle', 'rb') # for reading also binary mode is important
db=pickle.load(dbfile)
forkeysindb:
print(keys,'=>',db[keys])
dbfile.close()
if__name__=='__main__':
storeData()
loadData()