Python dict as unicode

Discussion in 'Python' started by Brendon, Nov 24, 2010.

  1. Brendon

    Brendon Guest

    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(128)

    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.
    Brendon, Nov 24, 2010
    #1
    1. Advertising

  2. Brendon

    Terry Reedy Guest

    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(128)
    >
    > 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
    Terry Reedy, Nov 25, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ethan Furman

    Python 3: dict & dict.keys()

    Ethan Furman, Jul 24, 2013, in forum: Python
    Replies:
    4
    Views:
    200
    Steven D'Aprano
    Jul 25, 2013
  2. Peter Otten

    Re: Python 3: dict & dict.keys()

    Peter Otten, Jul 24, 2013, in forum: Python
    Replies:
    1
    Views:
    86
    Neil Cerutti
    Jul 24, 2013
  3. Oscar Benjamin

    Re: Python 3: dict & dict.keys()

    Oscar Benjamin, Jul 24, 2013, in forum: Python
    Replies:
    0
    Views:
    98
    Oscar Benjamin
    Jul 24, 2013
  4. Peter Otten

    Re: Python 3: dict & dict.keys()

    Peter Otten, Jul 24, 2013, in forum: Python
    Replies:
    0
    Views:
    84
    Peter Otten
    Jul 24, 2013
  5. Skip Montanaro

    Re: Python 3: dict & dict.keys()

    Skip Montanaro, Jul 24, 2013, in forum: Python
    Replies:
    0
    Views:
    103
    Skip Montanaro
    Jul 24, 2013
Loading...

Share This Page