Unicode and exception strings

Terry Carroll carroll at tjc.com
Fri Jan 9 20:44:21 CET 2004

On 09 Jan 2004 13:18:39 +0100, Rune Froysa <rune.froysa at usit.uio.no>

>Assuming an exception like:
>  x = ValueError(u'\xf8')
>AFAIK the common way to get a string representation of the exception
>as a message is to simply cast it to a string: str(x).  This will
>result in an "UnicodeError: ASCII encoding error: ordinal not in
>The common way to fix this is with something like
>u'\xf8'.encode("ascii", 'replace').  However I can't find any way to
>tell ValueErrors __str__ method which encoding to use.

Rune, I'm not understanding what your problem is.

Is there any reason you're not using, for example, just repr(u'\xf8')?

In one program I have that occasionally runs into a line that includes
some (UTF-8) Unicode-encoded Chinese characters , I have something like

     _display_text = _display_text + "%s\n" % line
  except UnicodeDecodeError:
         # decode those UTF8 nasties
         _display_text = _display_text + "%s\n" % line.decode('utf-8'))
     except UnicodeDecodeError:
         # if that still doesn't work, punt
         # (I don't think we'll ever reach this, but just in case)
         _display_text = _display_text + "%s\n" % repr(line)

I don't know if this will help you or not.  

More information about the Python-list mailing list