Given an arbitrary picklable Python data structure data
, is
with open('a', 'bw') as f:
f.write(pickle.dumps(data))
equivalent to
with open('a', 'bw') as f:
pickle.dump(data, f)
i.e. can I assume this equivalence when writing code? Could you describe how you reached to this conclusion?
The use-case is to separate serialization from writing, since I may want to write pickle to a non-disk location.
The first two methods are used during the pickling process, and the other two are used during unpickling. The only difference between dump() and dumps() is that the first creates a file containing the serialization result, whereas the second returns a string.
Usually, you'd open the file in append ( "ab" ) mode to add data at the end. However, Pickle doesn't support appending, so you'll have to save your data to a new file (come up with a different file name -- ask the user or use a command-line parameter such as -o test. txt ?)
Python Pickle dump dump() function to store the object data to the file. pickle. dump() function takes 3 arguments. The first argument is the object that you want to store. The second argument is the file object you get by opening the desired file in write-binary (wb) mode.
Python pickle module is used for serializing and de-serializing a Python object structure. Any object in Python can be pickled so that it can be saved on disk. What pickle does is that it “serializes” the object first before writing it to file. Pickling is a way to convert a python object (list, dict, etc.)
Yes, they are equivalent. Looking at the source code of pickle.py
:
def _dump(obj, file, protocol=None, *, fix_imports=True):
_Pickler(file, protocol, fix_imports=fix_imports).dump(obj)
def _dumps(obj, protocol=None, *, fix_imports=True):
f = io.BytesIO()
_Pickler(f, protocol, fix_imports=fix_imports).dump(obj)
res = f.getvalue()
assert isinstance(res, bytes_types)
return res
dumps
does the exact same thing as dump
, just with an io.BytesIO
object instead of a file object. It calls the internal _Pickler().dump()
in the same way and simply returns the contents of the io.BytesIO
object. Therefore, all f.write(pickle.dumps(data))
does is first forward the result to an io.BytesIO
object then to the actual file instead of writing to the file directly.
They are not equivalent if an exception occurs during pickling. f.write(pickle.dumps(data))
will not write anything to the file. Whereas pickle.dump(data, f)
will end up with a snapped or partial pickle.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With