1. 程式人生 > >每日一讀:《 關於定義Python源代碼編碼 》

每日一讀:《 關於定義Python源代碼編碼 》

文字 nts unicode文件 magic nature file local beginning cape

官方pep原文:

Abstract:
This PEP proposes to introduce a syntax to declare the encoding of a Python source file. The encoding information is then used by the Python parser to interpret the file using the given encoding. Most notably this enhances the interpretation of Unicode literals in the source code and makes it possible to write Unicode literals using e.g. UTF-8 directly in an Unicode aware editor.

Problem:
In Python 2.1, Unicode literals can only be written using the Latin-1 based encoding "unicode-escape". This makes the programming environment rather unfriendly to Python users who live and work in non-Latin-1 locales such as many of the Asian countries. Programmers can write their 8-bit strings using the favorite encoding, but are bound to the "unicode-escape" encoding for Unicode literals.

Proposed Solution:
I propose to make the Python source code encoding both visible and changeable on a per-source file basis by using a special comment at the top of the file to declare the encoding. To make Python aware of this encoding declaration a number of concept changes are necessary with respect to the handling of Python source code data.

Defining the Encoding:
Python will default to ASCII as standard encoding if no other encoding hints are given. 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=
or (using formats recognized by popular editors):

#!/usr/bin/python
# -*- coding: -*-
or:

#!/usr/bin/python
# vim: set fileencoding= :
More precisely, the first or second line must match the following regular expression:

^[ \t\v]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)
The first group of this expression is then interpreted as encoding name. If the encoding is unknown to Python, an error is raised during compilation. There must not be any Python statement on the line that contains the encoding declaration. If the first line matches the second line is ignored.
To aid with platforms such as Windows, which add Unicode BOM marks to the beginning of Unicode files, the UTF-8 signature \xef\xbb\xbf will be interpreted as ‘utf-8‘ encoding as well (even if no magic encoding comment is given).
If a source file uses both the UTF-8 BOM mark signature and a magic encoding comment, the only allowed encoding for the comment is ‘utf-8‘. Any other encoding will cause an error.
翻譯:(因作者英語閱讀能力有限,以下只是作為閱讀參考)

抽象:
該PEP建議引入一種語法來聲明Python源文件的編碼。 Python解析器收到編碼信息然後使用給定的編碼解釋源文件。 最值得註意的是,這增強了源代碼中Unicode文字的解釋, 並且可以直接使用在Unicode編輯器中。例如:使用UTF-8編寫Unicode文字。

問題:
在Python 2.1中,Unicode文字只能使用基於Latin-1的編碼“unicode-escape”編寫。 這讓亞洲許多國家的Python用戶而言就不太友好。 程序員可以使用自己喜歡的編碼編寫他們的8位字符串, 但是綁定的Unicode文字是“unicode-escape”編碼。

建議的解決方案:
我建議通過在源文件頂部使用特殊註釋來聲明編碼格式, 使每個python源文件在源代碼裏的編碼格式都可見並且可以更改。
為了讓Python知道這個編碼聲明,需要對Python源代碼數據的處理進行一些概念上的改變。

定義編碼:
如果沒有給出其他編碼提示,Python將默認為ASCII作為標準編碼。
要定義源代碼編碼,必須將註釋作為文件的第一行或第二行放入源文件中,例如:

# coding =<你的編碼格式>
或者(使用大部分編輯器認可的格式):

#!/usr/bin/python
#-*- coding:<你的編碼格式> -*-
要麽:

#!/usr/bin/python
#vim:set fileencoding = <你的編碼格式>:
更確切地說,第一行或第二行必須符合以下正則表達式:

^[\t\v]*#.*?coding[:=][\t]*([-_.a-zA-Z0-9]+)
這個正則表達式組被解釋為編碼格式名稱。 如果是Python不知道的編碼,編譯的時候會產生錯誤。 記住。包含編碼格式聲明的行上面不能有任何的Python語句。否則第一行與第二行匹配則被忽略。
為了幫助平臺(如Windows)在Unicode文件的開頭添加Unicode BOM標記, (UTF-8標記) \xef\xbb\xbf 將被解釋為‘utf-8‘編碼(即使沒有給出編碼註釋)。
如果源文件同時使用UTF-8 BOM標記和編碼註釋, 則唯一允許的註釋編碼為‘utf-8‘。任何其他編碼都會導致錯誤。

總結:
這是一篇關於源文件編碼格式的建議。有我們常用到的一行代碼
# -*- coding:utf-8 -*-
這行代碼的意思是讓源文件支持中文註釋。
有一點我們要知道,python的源文件編碼格式支持是可更改的。
需要註意的是,以上討論中。關於中文編碼相關的問題只在python2系列版本中存在。 在python3中默認的編碼是Unicode,所以不需要在每個python文件中再加上
# -*- coding:utf-8 -*- 註釋。

每日一讀:《 關於定義Python源代碼編碼 》