473,795 Members | 3,006 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(pickl eFile = 'pickleTest.p') :
f = XWwz(pickleFile , 'rb')
up = pickle.Unpickle r(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.Unpick lingError: 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 5102
[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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
3687
by: Drochom | last post by:
Hello, I have a huge problem with loading very simple structure into memory it is a list of tuples, it has 6MB and consists of 100000 elements >import cPickle >plik = open("mealy","r") >mealy = cPickle.load(plik) >plik.close()
0
1144
by: paul | last post by:
hi list, I use cPickle in a small .cgi to save data. It run's fine on python 2.2.3 (used for development) but fails on 2.1.1 (webserver) with: File "spyblog.py", line 543, in __init__ self.entry = cPickle.load(open(log + self.filename, 'r+')) SystemError: Failed to import class _reconstructor from module copy_reg
3
1812
by: Chris Curvey | last post by:
Hi all, I have this program class Company: def __init__(self, revenues, costs): self.revenues = revenues self.costs = costs def __getattr__(self, name):
5
9454
by: Marcus Lowland | last post by:
Hello, I'm fairly new to python and have read about and wanted to begin experimenting with cpickle. As I understand, this should be a native module in the python library. I have python 2.3 and now just installed 2.4, but am not able to import or find cpickle.py in any directory of the install, or in the previous version (pickle.py is present and imports correctly). Is there a seperate module package that must be downloaded and installed......
4
2547
by: Martin Manns | last post by:
Hi If I cPickle a numpy array under Linux and un-cPickle it under Solaris 10, my arrays seem to be transposed. Is there any way to get the same behavior on both platforms without testing which platform the python script runs on and then transposing the array? I am using python 2.4.2 and numpy 0.9.6 on both machines. Thanks in advance
3
2363
by: aldonnelley | last post by:
Hi all. Just having a weird problem with tkinter. I'm trying to make a gui that shows results from an image search, with a "forward" and "back" button so the user can compare results from different pages. All that's working fine... The problem I'm having is that the images don't show onscreen the first time the "first page" of results shows up. When I click the "search again" button, and have more than the original results page to...
5
1513
by: rconradharris | last post by:
A co-worker of mine came across some interesting behavior in the Python interpreter today and I'm hoping someone more knowledgeable in Python internals can explain this to me. First, we create an instance of an Old-Style class without defining a __contains__ but instead define a __getitem__ method in which we raise KeyError. Next we repeatedly use the 'in' operator to test to see whether something, a string, an int, etc is an attribute...
7
6672
by: Chi Yin Cheung | last post by:
Hi, Is there a way in python to output binary files? I need to python to write out a stream of 5 million floating point numbers, separated by some separator, but it seems that all python supports natively is string information output, which is extremely space inefficient. I'd tried using the pickle module, but it crashed whenever I tried using it due to the large amount of data involved. Thanks for your help!
5
1765
by: Victor Kryukov | last post by:
Hello list, The following behavior is completely unexpected. Is it a bug or a by- design feature? Regards, Victor. -----------------
0
9522
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10448
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10217
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10167
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6784
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5566
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2922
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.