Skip to content

Commit 6dbd860

Browse files
committed
Python Pickle Module
1 parent 807bfcf commit 6dbd860

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Programs/P60_PickleModule.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Author: OMKAR PATHAK
2+
3+
# In this example we will see how to use pickle module for storing the data efficiently!
4+
# The pickle module translates an in-memory Python object into a serialized byte stream—a string of bytes
5+
# that can be written to any file-like object.
6+
7+
import pickle
8+
9+
def storeData():
10+
# initializing data to be stored in db
11+
Omkar = {'key' : 'Omkar', 'name' : 'Omkar Pathak', 'age' : 21, 'pay' : 40000}
12+
Jagdish = {'key' : 'Jagdish', 'name' : 'Jagdish Pathak', 'age' : 50, 'pay' : 50000}
13+
14+
# database
15+
db = {}
16+
db['Omkar'] = Omkar
17+
db['Jagdish'] = Jagdish
18+
19+
dbfile = open('examplePickle', 'ab') # Its important to use binary mode
20+
pickle.dump(db, dbfile) # source, destination
21+
dbfile.close()
22+
23+
def loadData():
24+
dbfile = open('examplePickle', 'rb') # for reading also binary mode is important
25+
db = pickle.load(dbfile)
26+
for keys in db:
27+
print(keys,'=>',db[keys])
28+
dbfile.close()
29+
30+
if __name__ == '__main__':
31+
storeData()
32+
loadData()

0 commit comments

Comments
 (0)