Unicode / UTF-8 Header for Python 2

2014-02-07 | #helper, #python

Although Python uses Unicode concepts extensively, the default file encoding is still ANSI in Python 2.X, so to be able to use all Unicode characters in your source, you'll have to define a coding header in each file, looking roughly like this:

# -*- coding: utf-8 -*-

from __future__ import unicode_literals

First line declares file encoding, second treats all strings as unicode strings by default, which is standard behaviour of Python 3 and should also be used in Python 2 at all time in my opinion.