[Python] Defining Python Source Code Encodings

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:

# coding=<encoding name>

or (using formats recognized by popular editors)

#!/usr/bin/python
# -*- coding: <encoding name> -*-

 

Examples
These are some examples to clarify the different styles for
defining the source code encoding at the top of a Python source
file:

1. With interpreter binary and using Emacs style file encoding
comment:

#!/usr/bin/python
# -*- coding: utf-8 -*-
import os, sys

#!/usr/bin/python
# -*- coding: latin-1 -*-
import os, sys

#!/usr/bin/python
# -*- coding: iso-8859-15 -*-
import os, sys

#!/usr/bin/python
# -*- coding: ascii -*-
import os, sys

2. Without interpreter line, using plain text:

# This Python file uses the following encoding: utf-8
import os, sys

3. Text editors might have different ways of defining the file’s
encoding, e.g.

#!/usr/local/bin/python
# coding: latin-1
import os, sys

4. Without encoding comment, Python’s parser will assume ASCII
text:

#!/usr/local/bin/python
import os, sys

5. Encoding comments which don’t work:

Place it on the first line

Missing „coding:“ prefix:

#!/usr/local/bin/python
# latin-1
import os, sys

Encoding comment not on line 1 or 2:

#!/usr/local/bin/python
#
# -*- coding: latin-1 -*-
import os, sys

Unsupported encoding:

#!/usr/local/bin/python
# -*- coding: utf-42 -*-
import os, sys

source: www.python.org

Schreibe einen Kommentar

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s