I often have data stored in python objects that aren’t dataframes.
Think config objects.
Saving and loading these data objects to and from disk is often very helpful.
The easiest way to save an arbitrary object is to use pickel
, however you almost always shouldn’t.
It is a security nightmare and in the worst case can lead to arbitrary code execution.
A safer alternative is to serialize the data:
import json
s = json.dumps(dict_object)
dict_obj = json.loads(s)
There are also dump
and load
that work directly with files and not string objects.
I have also recently come across pyserde.
A super cool library that allows the serialisation of dataclass
objects.