ソースの文字コード

Python 2.6.4

ファイルの文字コードが同じで、
ソースコードエンコーディングをそれぞれ指定した場合の確認


ファイルのエンコードを確認

machu@linea:test$ nkf -g *.py
e.py:UTF-8
n.py:UTF-8
s.py:UTF-8
u.py:UTF-8

デフォルトエンコードを確認

>>> import sys
>>> sys.getfilesystemencoding()
'UTF-8'
>>> sys.getdefaultencoding()
'ascii'

ソースコードエンコーディングをそれぞれ指定

e.py
#! /usr/bin/env python
# coding:euc

a="あいうえお"
print a
machu@linea:test$ python e.py 
  File "e.py", line 2
SyntaxError: encoding problem: with BOM
n.py
#! /usr/bin/env python

a="あいうえお"
print a
machu@linea:test$ python n.py 
  File "n.py", line 3
SyntaxError: Non-ASCII character '\xe3' in file n.py on line 3, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
s.py
#! /usr/bin/env python
# coding:Shift-Jis

a="あいうえお"
print a
machu@linea:test$ python s.py 
  File "s.py", line 4
SyntaxError: 'shift_jis' codec can't decode bytes in position 11-12: illegal multibyte sequence
u.py
#! /usr/bin/env python
# coding:utf-8

a="あいうえお"
print a
machu@linea:test$ python u.py 
あいうえお