Unicode and exception strings
Rune Froysa
rune.froysa at usit.uio.no
Wed Jan 14 02:35:17 EST 2004
Terry Carroll <carroll at tjc.com> writes:
> On 12 Jan 2004 08:41:43 +0100, Rune Froysa <rune.froysa at usit.uio.no>
> wrote:
>
> >Terry Carroll <carroll at tjc.com> writes:
> >
> >> On 09 Jan 2004 13:18:39 +0100, Rune Froysa <rune.froysa at usit.uio.no>
> >> wrote:
> >>
> >> >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
> >> >range(128)".
...
> >>> x = ValueError(u'\xf8')
> >>> x.args[0]
> u'\xf8'
I was aware of the args variable in Exception, though I could not find
any documentation for its usage, thus I wanted to rely on its internal
__str__ method, rather than constructing the message myself. But,
after a quick look at Python/exceptions.c, it seems that this is a
feasable way :-)
> The only thing is, what to do with it once you get there. I don't think
> 0xF8 is a valid unicode encoding on its own. IIRC, it's part of a
> multibyte character.
Python gives me this, so I think it is correct:
>>> unicode('ø', 'latin-1')
u'\xf8'
For my usage, "u'\xf8'.encode('latin-1', 'replace')" is sufficient.
> Is this helping any, or am I just flailing around?
It does, thanks a lot for your help.
Regards,
Rune Frøysa
More information about the Python-list
mailing list