import pickle
# dump : put the data of the object in a file
pickle.dump(obj, open(file_path, "wb"))
# dumps : return the object in bytes
data = pickle.dump(obj)
import pickle
a = {'hello': 'world'}
with open('filename.pickle', 'wb') as handle:
pickle.dump(a, handle, protocol=pickle.HIGHEST_PROTOCOL)
with open('filename.pickle', 'rb') as handle:
b = pickle.load(handle)
print a == b
import pickle
def pickling(path, data):
file = open(path,'wb')
pickle.dump(data,file)
def unpickling(path):
file = open(path, 'rb')
b = pickle.load(file)
return b
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
dummy_data = ['item1', 'item2', 'item3']
pickling('pickle_files/test.p', dummy_data)
output = unpickling('pickle_files/test.p')
print("Output=", output)
import pickle
a = {'hi': 'everybody'}
with open('filename.pickle', 'wb') as f:
pickle.dump(a, f, protocol=pickle.HIGHEST_PROTOCOL)
with open('filename.pickle', 'rb') as f:
b = pickle.load(f)
# Python3 program to illustrate store
# efficiently using pickle module
# 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.
import pickle
def storeData():
# 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
# Its important to use binary mode
dbfile = open('examplePickle', 'ab')
# source, destination
pickle.dump(db, dbfile)
dbfile.close()
def loadData():
# for reading also binary mode is important
dbfile = open('examplePickle', 'rb')
db = pickle.load(dbfile)
for keys in db:
print(keys, '=>', db[keys])
dbfile.close()
if __name__ == '__main__':
storeData()
loadData()