Turning a pickle string into a pickle list

Posted: July 2nd, 2007 | Author: | Filed under: python | No Comments »

Suppose we have a few pickled items, maybe even using different protocols:

import pickle
allPickles = ''
allPickles += pickle.dumps('test0', protocol = 0)
allPickles += pickle.dumps('test1', protocol = 1)
allPickles += pickle.dumps('test2', protocol = 2)

This would result in the following data:

"S'test0'\np0\n.U\x05test1q\x00.\x80\x02U\x05test2q\x00."

Here is an easy way to break the string apart into individual pickles:

pickles = []
import StringIO
sio = StringIO.StringIO(allPickles)
while sio.len > sio.pos:
    currentPos = sio.pos
    pickle.load(sio)
    pickles.append(sio.buf[currentPos:sio.pos])

Here is the resulting list:

["S'test0'\np0\n.", 
'U\x05test1q\x00.', 
'\x80\x02U\x05test2q\x00.']

Just to verify:

for item in pickles:
    print pickle.loads(item)

The result is correct:

test0
test1
test2

If this displays with correct syntax highlighting on the blog, then this was also a successful test of the wp-syntax plugin.



Leave a Reply