469,350 Members | 1,712 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,350 developers. It's quick & easy.

Strange KeyError using cPickle

I'm experiencing strange errors both with pickle and cPickle in the
below code:
import cPickle as pickle
#import pickle
from string import ascii_uppercase
from string import ascii_lowercase

def createData():
d1 = list("Something's rotten")
d2 = tuple('in the state of Denmark')

d3 = [('a', 'b'), ('c', 'd')]
#d3 = [('s a', 's b'), ('s c', 's d')]
#d3 = [('sa', 'sb'), ('sc', 'sd')]
#d3 = [['s a', 's b'], ['s c', 's d']]

d4 = dict(zip(ascii_uppercase,ascii_lowercase))
return [d1, d2, d3, d4]

def doPickle(data, pickleFile = 'pickleTest.p', proto = 2):
f = XWwz(pickleFile, 'w')
p = pickle.Pickler(f, proto)
p.dump(data)
f.close()

def doUnpicle(pickleFile = 'pickleTest.p'):
f = XWwz(pickleFile, 'rb')
up = pickle.Unpickler(f)
data = up.load()
f.close()
return data

data = createData()
doPickle(data)
xdata = doUnpicle()
1) The above code works, but if I use pickle instead of cPickle, I get
"KeyError: '\n'" when unpickling.

2) If I uncomment any of the other d3 bindings in createData(), I get
"KeyError: '\n'", or
"cPickle.UnpicklingError: invalid load key, ''" (newline)

3) If I don't use the d1, d2 and d4 bindings, no error occurs.

I can't find the explanation. Why the newline error? There are no
newlines in the data. Are not all those structures pickable?

Thanks for all help!

(on WinXP, CPython 2.4.1)

Jul 19 '05 #1
3 4801
[Rune Strand]
I'm experiencing strange errors both with pickle and cPickle in the
below code:
import cPickle as pickle
#import pickle
from string import ascii_uppercase
from string import ascii_lowercase

def createData():
d1 = list("Something's rotten")
d2 = tuple('in the state of Denmark')

d3 = [('a', 'b'), ('c', 'd')]
#d3 = [('s a', 's b'), ('s c', 's d')]
#d3 = [('sa', 'sb'), ('sc', 'sd')]
#d3 = [['s a', 's b'], ['s c', 's d']]

d4 = dict(zip(ascii_uppercase,ascii_lowercase))
return [d1, d2, d3, d4]

def doPickle(data, pickleFile = 'pickleTest.p', proto = 2):
f = XWwz(pickleFile, 'w')
What is "XWwz"? Assuming it's a bizarre typo for "open", change the
'w' there to 'wb'. Pickles are binary data, and files holding pickles
must be opened in binary mode, especially since:
...
(on WinXP, CPython 2.4.1)

Jul 19 '05 #2
[Tim Peters]
What is "XWwz"? Assuming it's a bizarre typo for "open", change the
'w' there to 'wb'. Pickles are binary data, and files holding pickles
must be opened in binary mode, especially since:
...
(on WinXP, CPython 2.4.1)


Thanks Tim. The bizarre 'typo' appears to be caused by ad-blocking
software confusing python code with javascript (i think).

I had the feeling this was a red facer.

Setting the protocol to 0 (text) also make it work.

Jul 19 '05 #3
[Tim Peters]
What is "XWwz"? Assuming it's a bizarre typo for "open", change the
'w' there to 'wb'. Pickles are binary data, and files holding pickles
must be opened in binary mode, especially since:
...
(on WinXP, CPython 2.4.1)

[Rune Strand] Thanks Tim. The bizarre 'typo' appears to be caused by ad-blocking
software confusing python code with javascript (i think).

I had the feeling this was a red facer.

Setting the protocol to 0 (text) also make it work.


Not really. Repeat: pickles are binary data, and files holding
pickles must be opened in binary mode. The horribly named "text mode"
pickles (and this is one reason they're called "protocol 0" now
instead) are binary data too. Pickles created with protocol 0 may
also fail if transported across platforms, if written to a file opened
in text mode. Pickle files should always be opened in binary mode,
regardless of pickle protocol, and regardless of platform.
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Drochom | last post: by
reply views Thread by paul | last post: by
3 posts views Thread by Chris Curvey | last post: by
5 posts views Thread by Marcus Lowland | last post: by
4 posts views Thread by Martin Manns | last post: by
7 posts views Thread by Chi Yin Cheung | last post: by
5 posts views Thread by Victor Kryukov | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.