LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-05-2011, 10:29 PM   #1
comp_brad1136
Member
 
Registered: Nov 2004
Location: SW Missouri, USA
Distribution: Debian 6 "Squeeze"
Posts: 55

Rep: Reputation: 7
python3 - trying to pickle object raises TypeError


I'm not sure where I'm going wrong.

Code:
import pickle
class theClass:
	attribute1 = None
	attribute2 = None
	attribute3 = None

instance = theClass()
instance.attribute1 = 'hello'
instance.attribute2 = 'hi'
instance.attribute3 = 'boo'

try:
	file = open('pickle', 'w')
	pickle.dump(instance, file)
	file.close()
except IOError as complaint:
	print('darn!')
	print(complaint)
generates:
Code:
Traceback (most recent call last):
  File "class.py", line 14, in <module>
    pickle.dump(instance, file)
  File "/usr/lib/python3.1/pickle.py", line 1345, in dump
    Pickler(file, protocol, fix_imports=fix_imports).dump(obj)
TypeError: must be str, not bytes
I'm using python3. What am I doing wrong?
 
Old 04-06-2011, 03:38 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well I am not 100% sure (and do not have P3 installed on this machine to test), but in the doco for pickle.dump is:

The file argument must have a write() method that accepts a single bytes argument. It can thus be an on-disk file opened for binary writing, a io.BytesIO instance, or any other custom object that meets this interface.

Whereas you have opened the file simply for writing, so maybe it needs to be:
Code:
file = open('pickle', 'wb')
Have you also tried to just write a simple string and then build to the class?
 
Old 04-06-2011, 07:37 AM   #3
comp_brad1136
Member
 
Registered: Nov 2004
Location: SW Missouri, USA
Distribution: Debian 6 "Squeeze"
Posts: 55

Original Poster
Rep: Reputation: 7
thanks! it worked!

After seeing the tidbit about the doc, I decided to look again. It mentions a couple times that pickle generates a byte stream. I didn't see where it tells you that the file must be a binary-mode file. However, way down at the very bottom, there is a printed example that shows that the fh is indeed 'wb'.

It reminds me of the "some features may vary on your model" statement in micro-print on the manual of modern microwaves.

Thank you again for your time!

http://docs.python.org/release/3.1.3....html#examples
 
  


Reply

Tags
error, python


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
TypeError: list indices must be integers felix001 Programming 1 12-27-2010 08:52 AM
python pickle dump and load jabfinger Programming 5 02-26-2009 07:02 AM
TypeError: rpmdb open failed Red Knuckles Fedora 8 11-05-2006 01:40 PM
A real lilo pickle legacyboy Mandriva 6 06-05-2006 02:25 PM
Resolution Pickle! Please Help! Geepak Linux - Newbie 4 02-08-2005 09:20 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration