Using pickle

https://docs.python.org/3/library/pickle.html

INTRODUCTION

The 'pickle' module implements binary protocols for serializing and de-serializing a Python object structure. 'Pickling' is the process whereby a Python object hierarchy is converted into a byte stream, and 'unpickling' is the inverse operation, whereby a byte stream is converted back into an object hierarchy.

Warning: The pickle module is not secure. Only unpickle data you trust. Safer serialization formats such as json may be more appropriate if you are processing untrusted data.


# pickle.dump(obj, file, protocol=None, *, fix_imports=True, buffer_callback=None)
# Write the pickled representation of the object 'obj' to the open file object 'file'.

# pickle.dumps(obj, protocol=None, *, fix_imports=True, buffer_callback=None)
# Return the pickled representation of the object 'obj' as a bytes object,
# instead of writing it to a file.

# pickle.load(file, *, fix_imports=True, encoding="ASCII", errors="strict", buffers=None)
# Read the pickled representation of an object from the open file object 'file'
# and return the reconstituted object hierarchy specified therein.

# pickle.loads(data, /, *, fix_imports=True, encoding="ASCII", errors="strict", buffers=None)
# Return the reconstituted object hierarchy of the pickled representation
# 'data' of an object. 'data' must be a bytes-like object.

import pickle

# prepare a collection of objects
adict = {"a": "piękny", "b": [1, 2.0, 3+4j], "c": {None, True, False}}

word = pickle.dumps(adict)   # pickling adict to bytes (b'...')

bdict = pickle.loads(word)   # unpickling from bytes

assert adict == bdict

import pickle

# prepare a collection of objects
adict = {"a": "piękny", "b": [1, 2.0, 3+4j], "c": {None, True, False}}

with open('data.pickle', 'wb') as outfile:   # a binary file
    pickle.dump(adict, outfile)   # pickling

with open('data.pickle', 'rb') as infile:   # a binary file
    bdict = pickle.load(infile)   # unpickling

assert adict == bdict