+2 votes
in Programming Languages by (74.9k points)

I want to save a python dictionary in a pickle file. My dictionary is as follows:

labels = {True:{1,2,3}, False:{4,5,6}}

How can I do?

1 Answer

+1 vote
by (353k points)
selected by
 
Best answer

Here is a sample code that you can try:

You have to use pickle.dump() to write your data to a pickle file. If you want to read the saved pickle file, you can use pickle.load()

>>> import pickle
>>> labels = {True:{1,2,3}, False:{4,5,6}}
>>> with open('abc.pkl', 'wb') as fh:
...     pickle.dump(labels, fh, protocol=pickle.HIGHEST_PROTOCOL)
...
>>> with open('abc.pkl', 'rb') as fh:
...     data = pickle.load(fh)
...
>>> data
{True: {1, 2, 3}, False: {4, 5, 6}}
>>>


...