Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Python dict as unicode

Reply
Thread Tools

Python dict as unicode

 
 
Brendon
Guest
Posts: n/a
 
      11-24-2010
Hi all,

I am trying to convert a dictionary to a unicode string and it fails
with an exception. I am awfully surprised but searching the web has
not turned up anything useful. I understand why the exception ocurrs,
but am not sure why this is the default behaviour of python and if
there is anything I can do to fix the problem.

I have a python dictionary:
d = { ......}

It contains both primitive and complex objects. I want a unicode
representation of that dict:
s = unicode(d)

Doing this I get an exception:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position
71: ordinal not in range(12

Now, it seems that unicode(d) is the same as unicode(str(d)). I was
expecting there to be a __unicode__ method in the dictionary that in
turn calls unicode() on each of the keys/values in the dict, but
apparently not. Instead it seems to call the equivalent of str() on
each key/value and then after adding them together, calls unicode() on
the resulting string.

Is this really the default behaviour? If so is there any way around
it?

I am using python 2.6.6 on a Linux system.
 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      11-25-2010
On 11/24/2010 5:58 PM, Brendon wrote:
> Hi all,
>
> I am trying to convert a dictionary to a unicode string and it fails
> with an exception. I am awfully surprised but searching the web has
> not turned up anything useful. I understand why the exception ocurrs,
> but am not sure why this is the default behaviour of python and if
> there is anything I can do to fix the problem.
>
> I have a python dictionary:
> d = { ......}
>
> It contains both primitive and complex objects. I want a unicode
> representation of that dict:
> s = unicode(d)
>
> Doing this I get an exception:
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position
> 71: ordinal not in range(12
>
> Now, it seems that unicode(d) is the same as unicode(str(d)). I was
> expecting there to be a __unicode__ method in the dictionary that in
> turn calls unicode() on each of the keys/values in the dict, but
> apparently not. Instead it seems to call the equivalent of str() on
> each key/value and then after adding them together, calls unicode() on
> the resulting string.
>
> Is this really the default behaviour? If so is there any way around
> it?


Use 3.x

> I am using python 2.6.6 on a Linux system.



--
Terry Jan Reedy

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dict.items() vs dict.iteritems and similar questions Drew Python 19 03-15-2007 09:23 PM
dict.has_key(x) versus 'x in dict' Paul Melis Python 48 12-15-2006 05:55 PM
Inconsistency in dictionary behaviour: dict(dict) not calling __setitem__ Almad Python 8 12-14-2006 07:37 PM
dict!ident as equivalent of dict["ident"] Alexander Kozlovsky Python 5 05-22-2006 08:06 AM
Re: dict->XML->dict? Or, passing small hashes through text? Skip Montanaro Python 0 08-15-2003 03:46 PM



Advertisments