[Tutor] Unicode problems
kent37 at tds.net
Tue Aug 29 23:09:34 CEST 2006
Ed Singleton wrote:
> I've been having unicode problems in python on Mac OS 10.4.
> I googled for it and found a good page in Dive Into Python that I
> thought might help
> I tried following the instructions and set my default encoding using a
> sitecustomize.py, but got the following:
>>>> import sys
>>>> s = u'La Pe\xf1a'
>>>> print s
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> UnicodeEncodeError: 'ascii' codec can't encode character u'\xf1' in
> position 5: ordinal not in range(128)
> As I understand it, that should work. I tried using different
> character sets (like latin-1, etc), but none of them work.
I'm not sure Dive into Python is correct. Here is what I get on Windows:
In : s = u'La Pe\xf1a'
In : print s
In : import sys
In : sys.getdefaultencoding()
In : sys.stdout.encoding
I think print converts to the encoding of sys.stdout, not the default
encoding. What is the value of sys.stdout.encoding on your machine?
> 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
More information about the Tutor