[Tutor] unicode help

Martin A. Brown martin at linux-ip.net
Sun May 29 00:00:34 CEST 2011


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