The Python Oracle

How do I read binary pickle data first, then unpickle it?

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Riding Sky Waves v001

--

Chapters
00:00 Question
01:10 Accepted answer (Score 8)
01:35 Answer 2 (Score 1)
01:53 Thank you

--

Full question
https://stackoverflow.com/questions/2764...

Question links:
[here]: http://bugs.python.org/issue3873

Accepted answer links:
[pickle.loads(string)]: http://docs.python.org/library/pickle.ht...

Answer 2 links:
[StringIO]: http://docs.python.org/library/stringio....

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #serialization #pickle

#avk47



ACCEPTED ANSWER

Score 8


pickle.load(file) expects a file-like object. Instead, use:

pickle.loads(string)

Read a pickled object hierarchy from a string. Characters in the string past the pickled object’s representation are ignored.




ANSWER 2

Score 1


The documentation mentions StringIO, which I think is one possible solution.

Try:

f = open("big_networkx_graph.pickle","rb")
bin_data = f.read()
sio = StringIO(bin_data)
graph_data = pickle.load(sio)