[ACCEPTED]-Pickle.dump to variable-pickle

Accepted answer
Score: 17

You are looking for an in-memory file object; in 9 Python 2 that's cStringIO.StringIO(), for Python 3 io.BytesIO(); these act 8 just like file objects and you can have 7 pickle.dump() write to these.

However, the easier path 6 would be to use pickle.dumps() to dump straight to a string 5 object instead.

Under the hood, what pickle.dumps() does for you is create 4 an in-memory file object, write the pickle 3 data to it and retrieve the string result 2 for you; see the source code:

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

but this way you don't 1 have to do that extra work yourself.

Score: 3

Here are the code snippets using pickle.dump() in case 3 you need:

Dumping from pickle_obj to bytes/string 2 variable

bytes_output = BytesIO()
pickle.dump(pickle_obj, model_bytes)
bytes_output_base64 = base64.b64encode(model_bytes.getvalue()).decode() # convert the bytes to base64 string
bytes_output.close()

Loading from a base64 string in 1 pickle_data to pickle_obj

pickle_bytes = BytesIO(base64.b64decode(pickle_data))
pickle_obj = pickle.loads(pickle_bytes.read())
pickle_bytes.close()

Hope it helps!

Score: 1

I know its late but I came across this same 5 issue and here's a solution that I came 4 up with. And I apologize for my bad English

add 3 this class to your code

class DataPacket:
    def __init__(self):
        pass

    def write(self, string):
        self._string = string

    @property
    def string(self):
        return self._string

and where u dump 2 your pickled result use this

    PickeledString = DataPacket()
    pickle.dump(dataJSON, PickeledString)

to read it use 1 this

PickeledString.string
Score: 1

I found a quick and clean explanation here on 1 stackoverlfow by Farhan K:

import pickle

mydict = { 'a': 1111, 'b': 2222 }
string_of_bytes_obj = str(pickle.dumps(mydict), encoding="latin1")

unpickled_dict = pickle.loads(bytes(string_of_bytes_obj, "latin1"))

More Related questions