In this tutorial you will learn how to save the contents of a dictionary to a pickle file (aka ‘pickling a file’). This will serialize your data objects by converting its contents into bytes. Make sure that you only un-pickle data coming from trusted sources.
Sample data for this example
We will start by creating some sample data that you can use in order to follow along this example.
# Create list object
year_lst = 2019, 2021, 2022
exp_lst = 240,340,400
rev_lst = 450, 800,700
# Create dictionary
performance_dict = dict(year = year_lst, expenses = exp_lst, revenue = rev_lst)
Save and serialize a dictionary with pickle
This script will help you convert your dictionary to a pickle file.
import pickle
#define a patch for the pickle file on your disk
pick_path = 'C:\\temp\\perf_data.pkl'
#convert the dictionary to pickle
with open (pick_path, 'wb') as pick:
pickle.dump(performance_dict, pick)
Note: attempting to open the file in write mode (w) instead will render a TypeError.
Load and read dictionary from pickle file
If you would like to read a pickle file into your Python program, proceed as following (remember: only read a pickle from a source you trust!)
#define an empty dictionary
performance_dict_unpacked ={}
# load the pickle contents
with open (pick_path,'rb') as pick:
performance_dict_unpacked.update(pickle.load(pick))
Note: also here, make sure that you open the pickle file in binary mode (‘rb’) instead of read (‘r’) mode. Otherwise you will receive a UnicodeDecocodeError – as your pickle file was written as bytes not as a string.
Write and load to Pickle with Pandas
You can use the pandas library to save and convert to pickle with Pandas. We have written a tutorial about writing to pickle, json and csv with pandas that you might want to read.
Serialize a list with pickle
Converting a Python list object to pickle can be done in a similar fashion:
pick_path = 'C:\\temp\\perf_list.pkl'
# dump a list to the pickle
with open (pick_path, 'wb') as pick:
pickle.dump(exp_lst, pick)
# define new list
exp_lst_unpacked =[]
You can de-serialize the list into a list of strings/floats in a very simple manner:
# append the pickle content to the list
with open (pick_path,'rb') as pick:
exp_lst_unpacked.append(pickle.load(pick))