[Tutor] unicode help

Marilyn Davis marilyn at pythontrainer.com
Sun May 29 00:17:04 CEST 2011


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