python create a save file using pickle code example

Example 1: create pickle file python

import pickle
file_name='my_file.pkl'
f = open(file_name,'wb')
pickle.dump(my_data,f)
f.close()

Example 2: write data to using pickle

import pickle

example_dict = {1:"6",2:"2",3:"f"}

pickle_out = open("dict.pickle","wb")
pickle.dump(example_dict, pickle_out)
pickle_out.close()

Example 3: load from file pickle

1 # Load the dictionary back from the pickle file.
   2 import pickle
   3 
   4 favorite_color = pickle.load( open( "save.p", "rb" ) )
   5 # favorite_color is now { "lion": "yellow", "kitty": "red" }