[Tutor] unicode help

Marilyn Davis marilyn at pythontrainer.com
Wed Nov 14 22:23:59 CET 2012


On Wed, November 14, 2012 1:07 pm, Marilyn Davis wrote:

> Thank you, Dave, for looking at my problem, and for correcting me on my
> top posting.
>
> See below:
>
>
> On Wed, November 14, 2012 12:34 pm, Dave Angel wrote:
>
>
>> On 11/14/2012 03:10 PM, Marilyn Davis wrote:
>>
>>
>>> Hi,
>>>
>>>
>>>
>>> Last year, I was helped so that this ran nicely on my 2.6:
>>>
>>>
>>>
>>> #! /usr/bin/env python
>>> # -*- coding: utf-8 -*-
>>> # necessary for python not to complain about "¥"
>>>
>>>
>>>
>>> symbol = unichr(165) print unicode(symbol)
>>>
>>> --- end of code ---
>>>
>>>
>>>
>>> But, now on my 2.7, and on 2.6 when I tried reinstalling it, I get:
>>>
>>>
>>>
>>> bash-3.2$ ./uni_test.py ./uni_test.py Traceback (most recent call
>>> last):
>>> File "./uni_test.py", line 6, in <module>
>>> print unicode(symbol) UnicodeEncodeError: 'ascii' codec can't encode
>>> character u'\xa5' in position 0: ordinal not in range(128) bash-3.2$
>>>
>>> Can anyone please help?  It says 'ascii' codec?  Shouldn't it be
>>> seeing 'utf-8'?
>>>
>>>
>>>
>>> I can't imagine it matters but I'm on an imac now and before I was on
>>>  Ubuntu.
>>>
>>>
>>>
>>> Thank you,
>>>
>>>
>>>
>>> Marilyn Davis
>>>
>>>
>>>
>>>
>>>
>>>
>>
>> You top-posted your message.  If you need to quote something, please
>> put your new text after what you're quoting.
>>
>> Try the following in your 2.7 interpreter:
>>
>>
>>
>>>>> import sys print sys.stdout.encoding
>> UTF-8
>>
>>
>>
>> If you don't see UTF-8, then there's a discrepancy between what you
>> think the terminal is doing, and what Python thinks.
>
> You're right, I get this:
>
>
> bash-3.2$ python Python 2.7.3 (v2.7.3:70274d53c1dd, Apr  9 2012, 20:52:43)
>  [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
>
>>>> import sys
> import sys
>>>> print sys.stdout.encoding
> print sys.stdout.encoding US-ASCII
>
>>>>
>
>
>>
>> Somebody familiar with the Mac might be able to tell you how to fix it
>> right, but you could try sys.stdout.encoding = "UTF-8"
>>
>> and see if it changes your symptoms.
>
> and, your good idea didn't work:
>
>>>> sys.stdout.encoding="UTF-8"
> sys.stdout.encoding="UTF-8" Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> TypeError: readonly attribute
>
>>>>
>
> Goodness!  I didn't expect it to be a Mac thing.
>
>
> So, on a Windows machine, running Python 2.6.6, sys.stdout.encoding is
> 'cp1252', yet the code runs fine.
>
>
> On Ubuntu with 2.7, it's 'UTF-8' and  it runs just fine.
>
>
> I find this most mysterious.
>
>
> Thank you for any help to get it running on my Mac.
>
>
> Marilyn
>

I found this site:
http://hints.macworld.com/article.php?story=20100713130450549

and that fixes it.

I would never guessed that!

Marilyn



More information about the Tutor mailing list