[Tutor] Decoding decleration

Kent Johnson kent37 at tds.net
Wed Nov 2 17:35:32 CET 2005


Johan Geldenhuys wrote:
> I receive this error when executing a file:
> "Non-ASCII character '\x8b' in file task.py on line 1, but no encoding 
> declared".
> 
> Can anybody tell me what the encoding is supposed to look like and is it 
> standard on all files that is trying to use Non-ASCII characters?

Python expects source files to be in ASCII unless they contain an encoding declaration. The encoding declaration is a special comment line. It must be the first or second line of the file and have the form
# -*- coding: <encoding name> -*-

for example
# -*- coding: iso-8859-1 -*-

The current release allows iso-8859-1 characters and gives a warning as you have seen. A future release will make this an error.

For full details see PEP 263:
http://www.python.org/peps/pep-0263.html

It's not explicit in the PEP but I assume the encoding name can be any name recognized by the Python codecs module; a list is here:
http://docs.python.org/lib/standard-encodings.html

Kent

-- 
http://www.kentsjohnson.com



More information about the Tutor mailing list