[Tutor] unicode help

Marilyn Davis marilyn at pythontrainer.com
Wed Nov 14 21:10:38 CET 2012


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








On Sat, May 28, 2011 2:17 pm, Marilyn Davis wrote:

> Thank you Martin,
>
>
> This:
>
>
> #!/usr/bin/env python
> # -*- coding: utf8 -*-
> '''Unicode handling for 2.6.
> '''
> [rest of module deleted]
>
>
> produces an emacs warning:
>
> Warning (mule): Invalid coding system `utf8' is specified
> for the current buffer/file by the :coding tag. It is highly recommended to
> fix it before writing to a file.
>
> But, if I save anyway, and run, I get this:
>
>
> ./uni.py
> File "./uni.py", line 13
> SyntaxError: 'utf8' codec can't decode byte 0xa5 in position 0: unexpected
>  code byte
>
> but, on a hunch, I tried
>
> # -*- coding: utf-8 -*-
>
>
> and emacs and python were very happy.  Thank you thank you thank you.
>
> Now I can enjoy my Saturday.
>
>
> Marilyn
>
>
>
>
>
> On Sat, May 28, 2011 3:00 pm, Martin A. Brown wrote:
>
>
>> Hello there,
>>
>>
>>
>> : I'm still on Python 2.6 and I'm trying to work some unicode
>> : handling.
>> :
>> : I've spent some hours on this snippet of code, trying to follow
>> : PEP 0263, since the error tells me to see it.  I've tried other
>> : docs too and I am still clueless.
>>
>>
>>
>> OK, so this is PEP 0263.  http://www.python.org/dev/peps/pep-0263/
>>
>>
>>
>> Did you miss these lines?
>>
>>
>>
>> To define a source code encoding, a magic comment must
>> be placed into the source files either as first or second line in the
>> file, such as:
>>
>> Or was it the lack of an explicit example for UTF-8 in the PEP?
>>
>>
>>
>> Try adding a single line to your script, as the second line.  That
>> should make your script look like:
>>
>> #! /usr/bin/env python
>> # -*- coding: utf8 -*-
>>
>>
>>
>> You might wonder why on earth you have to do this.  The interpreter
>> cannot safely assume that your editor (any arbitrary text editor) knows
>> how to create/save anything other than ASCII without this (slightly
>> hackish) hint.
>>
>> Good luck,
>>
>>
>>
>> -Martin
>>
>>
>>
>> --
>> Martin A. Brown
>> http://linux-ip.net/



More information about the Tutor mailing list