[Tutor] Unicode problems

Kent Johnson kent37 at tds.net
Thu Aug 31 16:57:03 CEST 2006


Ed Singleton wrote:
> On 8/29/06, Kent Johnson <kent37 at tds.net> wrote:
>>> The main problem  I am having is in getting python not to give an
>>> error when it encounters a sterling currency sign (£, pound sign here
>>> in UK), which I suspect might be some wider problem on the mac as when
>>> I type that character in the terminal it shows a # (but in Python it
>>> shows a £).
>>>       
>> Where is the pound sign coming from? What encoding is it in? What do you
>> mean, in Python it shows £? You said Python gives an error...Fixing your
>> first problem may not help this one without a bit more digging... (BTW
>> in the US a # is sometimes called a 'pound sign', maybe the computer is
>> trying to translate for you ;) - though it is for pound weight, not
>> pound sterling.)
>>     
>
> The pound sign is in the source code in a string, or in a text file I
> was reading in.  Both should be in utf-8 as I save all files to that
> by default.  I think it was (hopefully) just that python was choking
> on printing the character (I was printing everything for debugging
> purposes).
>   
You also need to tell Python that the file is in UTF-8 by putting an 
encoding declaration at the top of the file.

# -*- coding: utf-8 -*-

You probably want to make the strings Unicode strings as well, e.g. u'xxx'.
> If I type "£" into a text document and copy and paste it to the python
> console, it comes out as " £" (with a space).  If I copy and paste it
> back, the space is gone.
>   
Sounds like maybe you are pasting Unicode (two bytes) and the console 
interprets it as two characters.
> If I type "test £" (without quotes) into a text document and copy and
> paste it to the console it comes out as "#test" and goes to a new
> line, as if I had pressed enter.
>   
That on is very strange.

By the way you can explicitly control the conversion on output by using e.g.

print someString.encode('utf-8')

Finally, please keep the discussion on list.

Kent
> I'll keep digging and trying things out.
>
> Thanks
>
> Ed
>   




More information about the Tutor mailing list