Allowing for UTF-8 characters in Python source code

By default, Python cannot deal with non-ASCII characters in source files, such as the following line:

re.sub("ae", u'ä', "haette")
# SyntaxError: Non-ASCII character '\xc3' in file <file>.py on line <line>, but no 
# encoding declared; see for details

The file encoding can be signalized by adding a magic comment in either the first or second line of the file:

# -*- coding: utf-8 -*


  • [1] More information in the Python Enhancement Proposals index

Leave a Reply