import pickle
favorite_color = { "lion": "yellow", "kitty": "red" }
with open( "save.p", "wb" ) as f:
pickle.dump( favorite_color, f)
with open( "save.p", "rb" ) as f:
favorite_color = pickle.load(f)
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)
1 # Save a dictionary into a pickle file.
2 import pickle
3
4 favorite_color = { "lion": "yellow", "kitty": "red" }
5
6 pickle.dump( favorite_color, open( "save.p", "wb" ) )