The question may seem a little basic, but wasn't able to find anything that I understood in the internet. How do I store something that I pickled with dill?
I have come this far for saving my construct (pandas DataFrame, which also contains custom classes):
import dill
dill_file = open("data/2017-02-10_21:43_resultstatsDF", "wb")
dill_file.write(dill.dumps(resultstatsDF))
dill_file.close()
and for reading
dill_file = open("data/2017-02-10_21:43_resultstatsDF", "rb")
resultstatsDF_out = dill.load(dill_file.read())
dill_file.close()
but I when reading I get the error
TypeError: file must have 'read' and 'readline' attributes
How do I do this?
Just give it the file without the
read
:you can also dill to file like this:
So:
writes to a file directly. Whereas:
serializes
obj
and you can write it to file yourself.Likewise:
reads from a file, and:
constructs an object form a serialized object, which you could read from a file.
It is recommended to open a file using the
with
statement.Here:
has the same effect as:
The file will be closed as soon as you leave the indention of the
with
statement, even in the case of an exception.