How to get a unicode degrees symbol?

Russell E. Owen owen at astrono.junkwashington.emu
Wed Jun 6 13:04:38 EDT 2001


In article <zd8T6.1995$WW6.242950 at newsb.telia.net>,
 "Fredrik Lundh" <fredrik at pythonware.com> wrote:

>Russell E. Owen wrote:
>> I'm obvious missing some important bit of documentation, but I looked
>> through the manuals and can't seem to figure out how to get unicode
>> character constants into python strings.
>...
>not sure how you managed to miss this one:
>
>   http://www.python.org/doc/current/ref/strings.html
>
>...(more useful info omitted)
>
>if you want the same content, but want to print it in different ways
>depending on the platform's display encoding, you can use the encode
>method on the way out.  examples:
>
>    u = u"\N{DEGREE SIGN}"
>    print u.encode("iso-8859-1") # unix (us/western europe)
>    print u.encode("cp1252") # windows
>    print u.encode("macroman") # macintosh
>    print u.encode("cp850") # dos window
>
>if the target encoding doesn't have a degree sign, encode will raise
>an exception.  if you don't want that, pass "replace" or "ignore" as
>the second argument.

Thank you very much.

My main goal is to display the degree symbol (for example) on the 
current platform -- in a way that Tk, etc. can all handle it as a normal 
thing.

Apparently this requires encoding the data as an ascii string. OK, but 
surely there must be some simple way to do this that doesn't require a 
big case statement (if platform x then use encoding y...) and knowing 
the name of the most appropriate encoding for each platform?

-----

As to how I managed to miss the documentation link. I don't mean to 
sound ungrateful to anybody involved with Python, but in fact that was 
quite a battle when I first tried to figure it out. At the time I was 
using 2.0, but I retraced my steps today, just for the heck of it, using 
the 2.1 manuals.

I started with my local copy of "Python Documentation", the launching 
point for most or all documentation.

It lists the Global Module Index as being "for quick access to all 
documentation", so I started there (with some misgivings, since I knew 
strings were a built in type). The page of links sure looked promising, 
so I clicked on "string", but found myself in a description of the 
(obsolete, I think?) string module, with no pointers out to  unicode or 
the built in data type. An up link points to "string services" which at 
least has an entry for "unicodedata", but was still a dead end for more 
info about strings.

So my next try was the Python Library Reference. Alas, it's table of 
contents doesn't have an entry for "string". However, I knew enough 
about Python (barely) to try "Sequence Types", but I wasn't feeling very 
confident. 

That section does mention strings and has a link to the entry you 
suggested -- the string literal description in the Python Reference 
Manual. By the way, Python Documentation lists that as "Language 
Reference (for language lawyers)" -- not promising, and I wasn't even 
sure they were the same document until I did some hunting around.

At that point I knew how to generate unicode characters, but had no idea 
which ones were which. Not a big hassle -- I realized I could probably 
find unicode documentation on line fairly easily -- but another 
roadblock. Also, I did find a link to unicode today in retracing my 
steps, but don't quite remember where it was -- it's not in the Python 
Reference section on string literals.

Anyway, at that point I realized I could generate unicode characters, so 
I tried a few, just for fun. I quickly found I could not display them 
(for instance str() raised an exception. At that point I gave up, until 
my recent posting.

Far more than you wanted to know, I'm sure. With a few extra links 
sprinkled throughout that whole process could have been nicely shortcut 
with a somewhat happier ending. I'd be happy to add these myself if 
anybody is willing to receive the edited pages. (I can also use cvs -- 
as very much a neophyte -- but imagine I don't have checkin permission).

-- Russell



More information about the Python-list mailing list