简体   繁体   中英

Pickle.dump to variable

I'm new in python and i wanted to know if there is a solution for this problem:

I know that this may sound strange but i want to save the pickle.dump data into a variable. I begin to think that may i could bypass it by making a fake class to instead of writing in a file, writing in a variable:

class PickleDatatoVar(object):
    def __init__(self):
        self.data = None
    def write(self, data):
        self.data = data
    def get(self):
        return self.data

and then:

pick = PickleDatatoVar()
pickle.dump(Int, pick)
var = pick.get()

Nothing is presented as error, but the output is just a '.'

So is there a solution to instead of saving that in a file saving into a variable?

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

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

Under the hood , what pickle.dumps() does for you is create an in-memory file object, write the pickle data to it and retrieve the string result 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 have to do that extra work yourself.

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

Dumping from pickle_obj to bytes/string 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 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!

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

add 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 your pickled result use this

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

to read it use this

PickeledString.string

I found a quick and clean explanation here on 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"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM