[Tutor] Cannot cPickle.load()

Liam Clarke cyresse at gmail.com
Wed Dec 1 02:04:50 CET 2004


 myfilep=open("e:\Devel\listpickled", "r")
> >>> myfilep
> <open file 'e:\Devel\listpickled', mode 'r' at 0x00AA05A0>
> >>> mydict = cPickle.load(myfilep)

Have you tried - 

mypick = pickle.Unpickler(myfilep)
mydict =mypick.load()

I think that's how you do it.

On Tue, 30 Nov 2004 18:47:26 -0600, Hugo González Monteverde
<hugonz-lists at h-lab.net> wrote:
> 
> 
> Hi Tutors,
> 
> I think I must be missing something here. I pickled a dictionary of
> lists into a file, using protocol 2, some weeks ago.  Now I'm trying to
> load it and I can't.
> 
> File is 21kB long, so I know it has data in it, and when I read it into
> a string (without pickling) I understand the pickle is there. Here's  my
> log:
> 
> IDLE 1.0.3
> >>> import cPickle
> >>> import pickle
> >>> myfilep=open("e:\Devel\listpickled", "r")
> >>> myfilep
> <open file 'e:\Devel\listpickled', mode 'r' at 0x00AA05A0>
> >>> mydict = cPickle.load(myfilep)
> 
> Traceback (most recent call last):
>    File "<pyshell#4>", line 1, in -toplevel-
>      mydict = cPickle.load(myfilep)
> EOFError
> >>> myfilep.tell()
> 4096L
> >>> myfilep.seek(0)
> >>> mydict= pickle.load(myfilep)
> 
> Traceback (most recent call last):
>    File "<pyshell#7>", line 1, in -toplevel-
>      mydict= pickle.load(myfilep)
>    File "C:\Python23\lib\pickle.py", line 1390, in load
>      return Unpickler(file).load()
>    File "C:\Python23\lib\pickle.py", line 872, in load
>      dispatch[key](self)
>    File "C:\Python23\lib\pickle.py", line 1189, in load_binput
>      i = ord(self.read(1))
> TypeError: ord() expected a character, but string of length 0 found
> >>> myfilep.tell()
> 512L
> >>> myfilep.seek(0)
> >>> mystring = myfilep.read()
> >>> mystring
> '\x80\x02]q\x01(U\x05Aaronq\x02U\x04Abelq\x03U\x08Abelardoq\x04U\x07Abelinoq\x05U\x07Abigailq\x06U\x07Abrahamq\x07U\x05Abrilq\x08U\x06Abundiq\tU\x03Adaq\nU\x05Adahiq\x0bU\x06Adalayq\x0cU\tAdalbertoq\rU\x08Adalgisaq\x0eU\x06Adalidq\x0fU\x07Adamariq\x10U\x04Adelq\x11U\x05Adelaq\x12U\x08Adelaidaq\x13U\x07Adelinaq\x14U\x06Adizonq\x15U\x06Adolfoq\x16U\x06Adonayq\x17U\x06Adrianq\x18U\x07Adrianaq\x19U\x04Ad\xe1nq'
> >>> #But this is not the full contents of the file!
> >>> myfilep.tell()
> 20480L
> >>> len(mystring)
> 257
> >>> mystring = myfilep.read()
> >>> len(mystring)
> 157
> >>> mystring = myfilep.read()
> >>> len(mystring)
> 0
> >>> #ok, done with the reading
> >>>
> 
> I cannot make any sense of it. Both implementations of load() tell me
> they're encountering EOF right away. If I use read, there's the pickled
> data, but I cannot read the entire file into the variable.
> 
> Please help if possible. I don't know what elese to do... (!)
> 
> Hugo
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.


More information about the Tutor mailing list