Degree symbol (UTF-8 > ASCII)

Peter Clark pc451 at yahoo.com
Thu Apr 17 09:55:48 EDT 2003


Steven Taschuk <staschuk at telusplanet.net> wrote in message news:<mailman.1050548247.26781.python-list at python.org>...
> Quoth Peter Clark:
> > [...] However, when I put it into the code, I get this error:
> > 
> >     w += [deg.encode('UTF-8') + scale.strip()]
> > UnicodeError: ASCII decoding error: ordinal not in range(128)
> 
> Aha!  Yes, it's a *decoding* error.  Your 'deg' variable is a
> normal string, right?
    That's correct.

> >     Since the output is meant to be read to be displayed by a font
> > which is in essentially latin-1 encoding, I need to restrict the
> > manner in which the degree symbol is displayed to one byte. [...]
> 
> So just do something like
>     print >>fileobject, chr(176)
>     print >>fileobject, u'\N{DEGREE SIGN}'.encode('latin-1')

    It worked fine when printing to a file (i.e., from the prompt),
but I still got an error when I try to append it to a string destined
to be included in a list:

    w += [u'\N{DEGREE SIGN}'.encode('latin-1') + scale.strip()]
UnicodeError: ASCII decoding error: ordinal not in range(128)

    In this case, 'scale' is either 'C' or 'F' (hence the need for a
degree character). But then I realized that what you said above would
also apply to 'scale' as well. So when I changed the line to:

w += [u'\N{DEGREE SIGN}'.encode('latin-1') +
scale.strip().encode('latin-1')]

it worked perfectly. Problem solved, thank you very much! So in
retrospect, my mistake was in not paying attention to the encoding of
the rest of the string, which was why the error messages kept popping
up. Duely noted.




More information about the Python-list mailing list