[Tutor] unicode help

Marilyn Davis marilyn at pythontrainer.com
Sun May 29 00:07:30 CEST 2011


Thank you Alexandre for your quick reply.

I tried your suggestion (again) and I still get:

./uni.py
  File "./uni.py", line 20
SyntaxError: Non-ASCII character '\xa5' in file ./uni.py on line 21, but
no encoding declared; see http://www.python.org/peps/pep-0263.html for
details

Can you suggest a different encoding?  Or a way to figure out what it
should be?

Or am I making and re-making some stupid mistake?

I run on emacs under SUSE.

Marilyn

p.s.  The code is now:

#!/usr/bin/env python
# coding utf-8
'''Unicode handling for 2.6.
'''
class Currency(float):
    def __str__(self):
        value = self.__class__.symbol +  float.__str__(self)
        return value

class Yen(Currency):
    symbol = unichr(165)

def main():
    y = Yen(100)
    print unicode(y)

main()

"""
¥100.0
"""






On Sat, May 28, 2011 2:21 pm, Alexandre Conrad wrote:

> When Python loads your file from your file system, it assumes all
> characters in the file are ASCII. But when it hits non-ASCII characters
> (currency symbols), Python doesn't know how to interpret
> it. So you can give Python a hint by putting at the top of your file the
> encoding of your file:
>
> After the shebang (1st line), add the following comment:
> # coding: utf-8
>
>
> (or whatever encoding your file is saved to, I think it depends on
> your file system, usually utf-8 by default on Linux)
>
> HTH,
>
>
>
> 2011/5/28 Marilyn Davis <marilyn at pythontrainer.com>:
>
>> Hi,
>>
>>
>> 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.
>>
>> The code works, except for the comment at the end.
>>
>>
>> I would be very grateful for some help.
>>
>>
>>
>> #!/usr/bin/env python
>> '''Unicode handling for 2.6.
>> '''
>> class Currency(float):    def __str__(self):
>>        value = self.__class__.symbol +  float.__str__(self)
>>        return value
>>
>>
>> class Yen(Currency):    symbol = unichr(165)
>>
>>
>> class Pound(Currency):    symbol = unichr(163)
>>
>>
>> def main():    y = Yen(100)
>>    print unicode(y)
>>    p = Pound(100)
>>    print unicode(p)
>>
>>
>> main()
>>
>> """
>> ¥100.0
>> £100.0
>> """
>>
>>
>>
>>
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>>
>
>
>
> --
> Alex | twitter.com/alexconrad
>




More information about the Tutor mailing list