Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 23rd, 2004, 10:12 PM
TheSkunker TheSkunker is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 4 TheSkunker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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.

Reply With Quote
  #2  
Old January 24th, 2004, 05:20 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 2 m 16 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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


Reply With Quote
  #3  
Old January 24th, 2004, 10:44 AM
TheSkunker TheSkunker is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 4 TheSkunker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Nothing happened to the file I created when I pickled the stuff. Is there another way to store dictionaries?

Reply With Quote
  #4  
Old January 24th, 2004, 02:18 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 2 m 16 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #5  
Old January 24th, 2004, 11:55 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,536 Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level)Scorpions4ever User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 6 h 45 m 12 sec
Reputation Power: 876
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
__________________
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

Puzzle of the Month solved by Keath and KevinADC, superior perl programmers of the month
Looking for a perl job with kick-*** programmers in a well-known NASDAQ listed tech company with branches in the US and Europe? We're hiring. PM me for details. Requirements

Last edited by Scorpions4ever : January 25th, 2004 at 01:20 AM.

Reply With Quote
  #6  
Old January 25th, 2004, 09:22 AM
TheSkunker TheSkunker is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 4 TheSkunker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks,
I see what I was doing wrong.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Pickle problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway