Thread: Pickle problem

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    4
    Rep Power
    0

    Pickle problem


    Hi!
    When I try pickle.load(a) this is what happens:
    pickle.load(a)
    Traceback (most recent call last):
    File "<pyshell#14>", line 1, in ?
    pickle.load(a)
    File "C:\Python22\lib\pickle.py", line 982, in load
    return Unpickler(file).load()
    File "C:\Python22\lib\pickle.py", line 597, in load
    dispatch[key](self)
    File "C:\Python22\lib\pickle.py", line 657, in load_float
    self.append(float(self.readline()[:-1]))
    ValueError: invalid literal for float(): 1 for help

    Please help me.
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Mmmmm, could you attach your program or the pickle file so i can have a go? Sounds like theres may have been a problem when you created the pickle.

    We'll find out soon enough.

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    4
    Rep Power
    0
    Nothing happened to the file I created when I pickled the stuff. Is there another way to store dictionaries?
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    One very simple way to do this (works with any data) is to write the dictionaries to a python file and import it. Dont know why pickle didnt work..

    Mark.
    programming language development: www.netytan.com Hula

  8. #5
  9. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    Why not use cPickle instead of pickle. It's a lot faster than using pickle. Speaking of serialization, there are quite a few ways to do this in python. Three of these are:
    1. pickle
    2. cPickle
    3. marshal

    Some differences between them:
    1. Speed: marshal is the fastest, followed by cPickle, followed by pickle. The cPickle module is implemented in C as a python extension, whereas pickle is in pure python.
    2. Type storage: The marshal module can only be used to store python basic types (i.e.) numbers, strings, lists, tuples, dictionaries etc. You cannot use it to serialize classes. cPickle and pickle can handle the basic python types, as well as your classes.
    3. Portability: cPickle and pickle work across different releases of python. marshall is not guaranteed to work across different releases of python. This means that if you used marshal with one release of python to write a file, a different python release may not be able to read in the same file. However, marshal is guaranteed to be architecture independent, as long as both ends are running the same python release. cPickle and pickle are both architecture independent and release compatible.

    [edit] Added some code to save a dictionary using all three modules. BTW I think I might have an idea as to why you're having a problem with reading data back in. Are you using Windows or some other such OS that differentiates between binary and ascii files. This may cause problems if you don't open a file in binary mode (2.3 appears to have a fix?) and may be causing the problem you're seeing? Just a theory though... [/edit]
    Code:
    ========= Save dictionary using cPickle =======
    #!/usr/bin/env python
    
    import cPickle
    
    data = {1: 'foo', 5: 'bar', 42: ('foo', 'bar')}
    
    #Write out data
    out = open('output.dat', 'wb')
    cPickle.dump(data, out);
    out.close()
    
    # read it back in
    inp = open('output.dat', 'rb')
    data2 = cPickle.load(inp)
    inp.close()
    
    # print the data
    print data2
    
    ========= Save dictionary using pickle =======
    #!/usr/bin/env python
    
    import pickle
    
    data = {1: 'foo', 5: 'bar', 42: ('foo', 'bar')}
    
    #Write out data
    out = open('output.dat', 'wb')
    pickle.dump(data, out);
    out.close()
    
    # read it back in
    inp = open('output.dat', 'rb')
    data2 = pickle.load(inp)
    inp.close()
    
    # print the data
    print data2
    
    ========= Save dictionary using marshal =======
    #!/usr/bin/env python
    
    import marshal
    
    data = {1: 'foo', 5: 'bar', 42: ('foo', 'bar')}
    
    #Write out data
    out = open('output.dat', 'wb')
    marshal.dump(data, out);
    out.close()
    
    # read it back in
    inp = open('output.dat', 'rb')
    data2 = marshal.load(inp)
    inp.close()
    
    # print the data
    print data2
    Last edited by Scorpions4ever; January 25th, 2004 at 02:20 AM.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    4
    Rep Power
    0
    Thanks,
    I see what I was doing wrong.

IMN logo majestic logo threadwatch logo seochat tools logo