Steven Taschuk
2003-04-17 19:06:05 UTC
[...]
but I still got an error when I try to append it to a string destined
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?
'\xb0C'
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.
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),print >>fileobject, chr(176)
print >>fileobject, u'\N{DEGREE SIGN}'.encode('latin-1')
but I still got an error when I try to append it to a string destined
w += [u'\N{DEGREE SIGN}'.encode('latin-1') + scale.strip()]
UnicodeError: ASCII decoding error: ordinal not in range(128)
'\xb0' + 'C'
'\xb0' + u'C'
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 )
" (
Steven Taschuk o- @
staschuk at telusplanet.net 7O )
" (