定义Python源代码编码的正确方法定义如何声明Python源代码编码。通常,Python文件的前2行应该以下内容开头:#!/usr/bin/python# -*- coding: <encoding name> -*-但我看过很多文件,开头是:#!/usr/bin/python# -*- encoding: <encoding name> -*-=> 编码而不是编码.那么,声明文件编码的正确方式是什么呢?是编码因为所用的正则表达式是懒惰的?或者它只是声明文件编码的另一种形式?我问这个问题是因为PEP没有提到编码,它只是在谈论编码.
3 回答
慕妹3242003
TA贡献1824条经验 获得超6个赞
coding[=:]\s*([-\w.]+)
# -*- coding: <encoding-name> -*-
# vim:fileencoding=<encoding-name>
coding
隔江千里
TA贡献1906条经验 获得超10个赞
第一行或第二行必须匹配正则表达式“编码[:=]\s*([-\w.]+)”
#!/usr/bin/python# vim: set fileencoding=<encoding name> :
# This Python file uses the following encoding: utf-8import os, sys
HUWWW
TA贡献1874条经验 获得超12个赞
要定义源代码编码,必须将一个神奇的注释作为文件中的第一行或第二行放在源文件中,例如: # coding=<encoding name>
或(使用受欢迎编辑认可的格式): #!/usr/bin/python# -*- coding: <encoding name> -*-
或: #!/usr/bin/python# vim: set fileencoding=<encoding name> :
更准确地说,第一行或第二行必须匹配以下正则表达式: ^[ \t\f]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)
coding
encoding
coding
coding
添加回答
举报
0/150
提交
取消