Home > database >  Converting the output of pickle.dumps() into a string and back?
Converting the output of pickle.dumps() into a string and back?

Time:11-28

In my Python program, I have a list with some objects from a custom class:

# Some example program, not the actual code.
class SomeClass:
    def __init__(self):
        import random
        import os
        self.thing = random.randint(5,15)
        self.thing2 = str(os.urandom(16))
        self.thing3 = random.randint(1,10)
        self.thing4 = "You get the idea"
a_list = [SomeClass(),SomeClass(),SomeClass(),SomeClass(),SomeClass()]
import pickle
print((pickle.dumps(a_list)))

I need to convert the output of pickle.dumps() into a string, which is easy enough, but how do I convert that back into a byte stream that pickle.loads() can use? Help would be appreciated.

  • I tried converting it into UTF-8, but since the file I need to save the string to is UTF-8 encoded, so that did not work.

CodePudding user response:

The usual way to "stringify" binary data is to base64-encode it:

>>> import pickle
>>> import base64
>>> L = list(range(5))
>>> ps = pickle.dumps(L)
>>> ps
b'\x80\x04\x95\x0f\x00\x00\x00\x00\x00\x00\x00]\x94(K\x00K\x01K\x02K\x03K\x04e.'
>>> s = base64.b64encode(ps).decode('ascii')
>>> s
'gASVDwAAAAAAAABdlChLAEsBSwJLA0sEZS4='
>>># Round trip
>>> pickle.loads(base64.b64decode(s))
[0, 1, 2, 3, 4]

Base64 encoding is usually used for transferring binary data in text-only environments (such as HTTP). However if you want to save you pickled data to a file you can do this directly by opening the file in binary mode:

with open('myfile.bin', 'wb') as f:
    pickle.dump(my_object, f)
  • Related