Degree symbol (UTF-8 > ASCII)

Duncan Booth duncan at
Thu Apr 17 10:52:47 CEST 2003

pc451 at (Peter Clark) wrote in 
news:f7199550.0304161720.702ac0f6 at

>     w += [chr(176) + scale.strip()]
> UnicodeError: ASCII decoding error: ordinal not in range(128)
>     I've tried changing its encoding to 'utf-8' as well with
> chr(176).encode('utf-8'), but that just returns the same error. This
> doesn't make sense: if 'print chr(176)' works fine, why doesn't it
> work later on? In a nut shell, all I want to do is add the degree
> character to a string contained within a list. That doesn't seem hard
> at all, but I'm completely stumped.

A latin1 string containing chr(176) is a degree symbol. This string is 
already encoded (as latin-1), but Python doesn't know that until you tell 
it. If you were to do 'print chr(176)' in a command window on the machine 
I'm typing on, you get a funny graphic character because the output is 
using codepage 437 not latin1.
If you want to change its encoding, e.g. to utf-8, you must first DECODE it 
from latin1 (giving you a unicode string) and then ENCODE it to utf8.

>>> chr(176).decode('latin1').encode('utf8')

(Or in my case I can do "print chr(176).decode('latin1').encode('cp437')" 
and see a degree symbol.)

Duncan Booth                                             duncan at
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?

More information about the Python-list mailing list