[Tutor] unicode help

Dave Angel d at davea.name
Wed Nov 14 21:34:35 CET 2012


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.

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.


-- 

DaveA



More information about the Tutor mailing list