Python 2.xコード宣言:coding:utf-8かcoding=urf-8か
2706 ワード
私の一般的なLinuxの書き方は:
Pythonソースのヘッダファイルで符号化方法を宣言することを知っています.asciiコードだけでなく、多くの人が少し違います.
#coding=utf-8
#coding:utf-8
#-*- coding:utf-8 -*-
では、どのように書くのが効果的なのか、どのようなメリットが無効なのか.
表示できるhttp://www.python.org/dev/peps/pep-0263/の説明
ざっと見てみましょう.
このPEPの目的は、Pythonソースファイルでコードを宣言する構文を紹介することです.その後、Python解釈器は、ファイルを解釈する際にこれらの符号化情報を使用する.最も顕著なのは、ソースファイルにおけるUnicodeの解釈であり、Unicodeを識別できるエディタでFUT-8のような符号化を使用することが可能になる.
Pythonで他の符号化方式を宣言していない場合、ASCII符号化を標準符号化方式としてソースファイルの符号化方式を定義するために、魔法の宣言はこのファイルの第1行または第2行に置かなければならない.例えば、
[python] #coding=
または(ポピュラーエディタのフォーマットを使用)
[python] #!/usr/bin/python # -*- coding: -*-
または
[python] #!/usr/bin/python # vim: set fileencoding= :
いずれにしても、最初の行または2番目の行の宣言は正規表現に合致します.
[python] "coding[:=]\s*([-\w.]+)"
だから、なぜコロンや等号を使うのかを知ることができます.宣言された符号化pythonが認識できないと、エラーが発生します.
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: 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: 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 ...
以上の例では、正しい書き方と正しい書き方を十分に説明しています.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
Pythonソースのヘッダファイルで符号化方法を宣言することを知っています.asciiコードだけでなく、多くの人が少し違います.
#coding=utf-8
#coding:utf-8
#-*- coding:utf-8 -*-
では、どのように書くのが効果的なのか、どのようなメリットが無効なのか.
表示できるhttp://www.python.org/dev/peps/pep-0263/の説明
ざっと見てみましょう.
概要:
このPEPの目的は、Pythonソースファイルでコードを宣言する構文を紹介することです.その後、Python解釈器は、ファイルを解釈する際にこれらの符号化情報を使用する.最も顕著なのは、ソースファイルにおけるUnicodeの解釈であり、Unicodeを識別できるエディタでFUT-8のような符号化を使用することが可能になる.
どうやって宣言しますか?
Pythonで他の符号化方式を宣言していない場合、ASCII符号化を標準符号化方式としてソースファイルの符号化方式を定義するために、魔法の宣言はこのファイルの第1行または第2行に置かなければならない.例えば、
[python]
または(ポピュラーエディタのフォーマットを使用)
[python]
または
[python]
いずれにしても、最初の行または2番目の行の宣言は正規表現に合致します.
[python]
だから、なぜコロンや等号を使うのかを知ることができます.宣言された符号化pythonが認識できないと、エラーが発生します.
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: 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: 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 ...
以上の例では、正しい書き方と正しい書き方を十分に説明しています.