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

Question:

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.
Asked By: WinFan3672

||

Answers:

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'x80x04x95x0fx00x00x00x00x00x00x00]x94(Kx00Kx01Kx02Kx03Kx04e.'
>>> 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)
Answered By: snakecharmerb
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.