Search⌘ K
AI Features

Loading Data from a Pickle File

Explore how to load data from pickle files in Python using the pickle module. Learn to open files in binary mode, deserialize objects with pickle.load, and recreate original data structures accurately. Understand the concept of data equality versus identity through practical coding examples.

We'll cover the following...

Now switch to your second Python Shell — i.e. not the one where you created the entry dictionary.

Python 3.5
shell = 2
print (shell) #①
#2
print (entry) #②
#Traceback (most recent call last):
# File "/usercode/__ed_file.py", line 5, in <module>
# print (entry) #\u2461
#NameError: name 'entry' is not defined
Python 3.5
import pickle
with open('entry.pickle', 'rb') as f: #③
entry = pickle.load(f) #④
print (entry) #⑤
#{'comments_link': None,
# 'internal_id': b'\xDE\xD5\xB4\xF8',
# 'title': 'Dive into history, 2009 edition',
# 'tags': ('diveintopython', 'docbook', 'html'),
# 'article_link':
# 'http://diveintomark.org/archives/2009/03/27/dive-into-history-2009-edition',
# 'published_date': time.struct_time(tm_year=2009, tm_mon=3, tm_mday=27, tm_hour=22, tm_min=20, tm_sec=42, tm_wday=4, tm_yday=86, tm_isdst=-1),
# 'published': True}

① This is Python Shell #2.

② There is no entry variable defined here. You defined an entry variable in Python Shell #1, but that’s a completely different environment with its own state.

③ Open the entry.pickle file you created in Python Shell #1. ...