Degree symbol (UTF-8 > ASCII)

Peter Clark pc451 at
Thu Apr 17 03:32:06 CEST 2003

Irmen de Jong <irmen at> wrote in message news:<3e9daad2$0$49115$e4fe514c at>...

> The "degree" symbol is chr(176) in what character encoding?
> Certainly not in UTF-8. Perhaps in windows cp-1252 or ISO-8859-1.
    Yes, latin-1.
> (if you look what is written you'll see that the character
> is encoded in two bytes when using UTF-8: 0xc2,0xb0)
    This works in the shell, expect that 'print
degreeChar.encode('UTF-8')' returns two characters, '°' Well,
obviously, since it's Unicode being displayed in a latin-1
environment. (Strictly speaking, sys.getdefaultencoding() returns
'ascii', but the interactive prompt still prints characters greater
than 128.) 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)

    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. Yet I
cannot get it to behave, even though 'print chr(176) works perfectly
fine at the prompt. My suspicion is that the default encoding of the
system is messing python up somewhere along the way--is there any way
to tell it to just print the stupid character and not be concerned
with the output?

More information about the Python-list mailing list