Degree symbol (UTF-8 > ASCII)
Steven Taschuk
staschuk at telusplanet.net
Thu Apr 17 15:06:05 EDT 2003
Quoth Peter Clark:
> Steven Taschuk <staschuk at telusplanet.net> wrote in message news:<mailman.1050548247.26781.python-list at python.org>...
[...]
> > 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)
And scale is a Unicode string, right?
>>> '\xb0' + 'C'
'\xb0C'
>>> '\xb0' + u'C'
Traceback (most recent call last):
File "<stdin>", line 1, in ?
UnicodeError: ASCII decoding error: ordinal not in range(128)
(For the same reason as before: concatenating a normal string and
a Unicode string results in a Unicode string, which means that the
normal string has to be decoded into a Unicode string first.)
I'd suggest doing all your string assembly with Unicode strings,
and only encoding to ISO-8859-1 at the output stage. Mixing the
two kinds of strings is asking for trouble, as you have seen.
--
Steven Taschuk o- @
staschuk at telusplanet.net 7O )
" (
More information about the Python-list
mailing list