Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading