Pickle files¶
- We have seen how to write and read raw data to files using numpy and basic file I/O.
- Another method involves directly saving variables and reading them back in.
- Use a so-called pickle file.
- These will be binary, so you can't read the data in a text editor, but you can save and load the file.
Write the file¶
In [18]:
import numpy as np
import pickle
description = "x-y data"
x = np.linspace(0,3,1000)
y = np.sin(20*x)*np.exp(-2*x)
with open('data.pkl', 'wb') as ofile: # 'wb' for writing binary
pickle.dump(description, ofile)
pickle.dump(x, ofile)
pickle.dump(y, ofile)
Read from the file¶
- Read variables in the same order they were written
In [19]:
with open('data.pkl', 'rb') as ifile: # 'rb' for reading binary
description = pickle.load(ifile)
x = pickle.load(ifile)
y = pickle.load(ifile)
Preferred method: use a dictionary¶
- To avoid worrying about how the data is stored when you read the file (which likely happens in codes other than what was used to write the data), use a dictionary.
- In building the dictionary, the keys don't have to match the variable names.
- It's not a bad idea to include a description variable.
- Triple quoted strings are nice for this since they can include more formatting.
- Could even format using markdown and then render the results, including equations, etc.
- Triple quoted strings are nice for this since they can include more formatting.
In [25]:
#------------------ wrap the data
the_data = { "description": description, "x":x, "y":y }
#------------------ write the file
with open('data.pkl', 'wb') as ofile:
pickle.dump(the_data, ofile)
#------------------ read the file
with open('data.pkl', 'rb') as ifile:
my_data = pickle.load(ifile)
my_data.keys() # output the keys (the data availabe in the pickle file)
Out[25]:
dict_keys(['description', 'x', 'y'])
In [26]:
plt.plot(my_data['x'], my_data['y'])
Out[26]:
[<matplotlib.lines.Line2D at 0x139bb02f0>]
In [ ]: