[Tutor] unicode help

Alexandre Conrad alexandre.conrad at gmail.com
Sat May 28 23:21:28 CEST 2011


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