January 23rd, 2004, 10:12 PM
-
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.
January 24th, 2004, 05:20 AM
-
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.
January 24th, 2004, 10:44 AM
-
Nothing happened to the file I created when I pickled the stuff. Is there another way to store dictionaries?
January 24th, 2004, 02:18 PM
-
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.
January 24th, 2004, 11:55 PM
-
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 01: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
January 25th, 2004, 09:22 AM
-
Thanks,
I see what I was doing wrong.