【转】关于Python脚本开头两行的:#!/usr/bin/python和# -*- coding: utf-8 -*-的作用 – 指定文件编码类型...
原文網(wǎng)址:http://www.crifan.com/python_head_meaning_for_usr_bin_python_coding_utf-8/
#!/usr/bin/python
是用來說明腳本語言是python的
是要用/usr/bin下面的程序(工具)python,這個(gè)解釋器,來解釋python腳本,來運(yùn)行python腳本的。
?
# -*- coding: utf-8 -*-
是用來指定文件編碼為utf-8的
詳情可以參考:
PEP 0263 — Defining Python Source Code Encodings
?
在此,詳細(xì)的(主要是翻譯)解釋一下,為何要加這個(gè)編碼聲明,以及如何添加編碼聲明:
?
使用文件編碼聲明以前所遇到的問題
Python 2.1中,想要輸入U(xiǎn)nicode字符,只能用基于Latin-1的"unicode-escape"的方式輸入 -> 對于其他非Latin-1的國家和用戶,想要輸入U(xiǎn)nicode字符,就顯得很繁瑣,不方便。
希望是:
編程人員,根據(jù)自己的喜好和需要,以任意編碼方式輸入字符串,都可以,這樣才正常。
?
建議選用的方案
所以,才有人給Python官方建議,所以才有此PEP 0263。
此建議就是:
允許在Python文件中,通過文件開始處的,放在注釋中的,字符串形式的,聲明,聲明自己的python文件,用何種編碼。
由此,需要很多地方做相應(yīng)的改動(dòng),尤其是Python文件的解析器,可以識別此種文件編碼聲明。
?
具體如何聲明python文件編碼?
上面已經(jīng)說了,是,文件開始處的,放在注釋中的,字符串形式的,聲明。
那具體如何聲明,以什么樣的格式去聲明呢?
其實(shí)就是,你之前就見過的,這種:
?| 1 | # -*- coding: utf-8 -*- |
對此格式的詳細(xì)解釋是:
- 如果你沒聲明編碼,但是文件中又包含非ASCII編碼的字符的話,python解析器去解析的python文件,自然就會(huì)報(bào)錯(cuò)了。
| 1 | # coding=<encoding name> |
| 1 2 | #!/usr/bin/python # -*- coding: <encoding name> -*- |
| 1 2 | #!/usr/bin/python # vim: set fileencoding=<encoding name> : |
- 符合正則表達(dá)式: ?
1 "coding[:=]\s*([-\w.]+)" - 的都可以,很明顯,如果你熟悉正則表達(dá)式,也就可以寫出來,其他一些合法的編碼聲明,以utf-8為例,比如:
| 1 2 3 4 5 | coding:???????? utf-8 coding=utf-8 coding=??????????????????utf-8 encoding:utf-8 crifanEncoding=utf-8 |
?
文件編碼聲明的各種例子
針對上面的規(guī)則,下面給出各種,合法的,非法的,例子,供參考:
合法的python文件編碼聲明
| 1 2 3 4 | #!/usr/bin/python # -*- coding: latin-1 -*- import?os, sys ... |
| 1 2 3 4 | #!/usr/bin/python # -*- coding: iso-8859-15 -*- import?os, sys ... |
| 1 2 3 4 | #!/usr/bin/python # -*- coding: ascii -*- import?os, sys ... |
| 1 2 3 | # This Python file uses the following encoding: utf-8 import?os, sys ... |
| 1 2 3 4 | #!/usr/local/bin/python # coding: latin-1 import?os, sys ... |
- 很明顯,其中的沒用-*-,直接用了coding加上編碼值
| 1 2 3 | #!/usr/local/bin/python import?os, sys ... |
非法的python文件編碼聲明舉例
| 1 2 3 4 | #!/usr/local/bin/python # latin-1 import?os, sys ... |
| 1 2 3 4 5 | #!/usr/local/bin/python # # -*- coding: latin-1 -*- import?os, sys ... |
| 1 2 3 4 | #!/usr/local/bin/python # -*- coding: utf-42 -*- import?os, sys ... |
?
python文件編碼聲明所遵循的理念
1.單個(gè)的完整的python源碼文件中,只用單一的編碼。
->
不允許嵌入了多種的編碼的數(shù)據(jù)
否則會(huì)導(dǎo)致(python解釋器去解析你的python文件時(shí))報(bào)編碼錯(cuò)誤。
?
不太懂這段:
| Any encoding which allows processing the first two lines in the way indicated above is allowed as source code encoding, this includes ASCII compatible encodings as well as certain multi-byte encodings such as Shift_JIS. It does not include encodings which use two or more bytes for all characters like e.g. UTF-16. The reason for this is to keep the encoding detection algorithm in the tokenizer simple. |
?
2.這段也不太懂:
| Handling of escape sequences should continue to work as it does now, but with all possible source code encodings, that is standard string literals (both 8-bit and Unicode) are subject to escape sequence expansion while raw string literals only expand a very small subset of escape sequences. |
?
3.Python的分詞器+編譯器,會(huì)按照如下的邏輯去工作:
要注意的是:
Python中的標(biāo)識符,都是ASCII的。
?
其余的內(nèi)容,不翻譯了。
至此,已經(jīng)解釋的夠清楚了。
?
轉(zhuǎn)載于:https://www.cnblogs.com/wi100sh/p/4238933.html
總結(jié)
以上是生活随笔為你收集整理的【转】关于Python脚本开头两行的:#!/usr/bin/python和# -*- coding: utf-8 -*-的作用 – 指定文件编码类型...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 说王一博不好帖子都没了
- 下一篇: 王者好玩的点在哪啊?