# -*- coding:utf-8 -*- 为什么要这样的格式?

2013-07-15 22:14:19 +08:00
 paicha
「 -*- 」有什么意义么?

同时发现这样写也是正常运行的:
# coding:utf-8
这样不是更简单么?
13613 次点击
所在节点    Python
17 条回复
pubby
2013-07-15 22:23:34 +08:00
emacs会处理首行的这些“设置”信息

最常见的就是这个编码设定

格式就是在文件首行写上:
-*- ......... -*-
pubby
2013-07-15 22:25:39 +08:00
还有一种是写在文件末尾


比如:
/* ----- for emacs ----- */
/* Local Variables: */
/* mode: php */
/* coding: utf-8-unix */
/* tab-width: 4 */
/* c-basic-offset: 4 */
/* indent-tabs-mode: nil */
/* End: */
paicha
2013-07-15 22:29:25 +08:00
@pubby 噢噢,那么用 # -*- coding:utf-8 -*- 可以理解为比 # coding:utf-8 兼容性更好么?
keakon
2013-07-15 22:30:02 +08:00
那是 emacs 设置 encoding 的风格。
http://www.python.org/dev/peps/pep-0263/
pubby
2013-07-15 22:36:17 +08:00
sinxccc
2013-07-15 22:38:05 +08:00
这是给 Emacs 看的。
paicha
2013-07-15 22:47:01 +08:00
@pubby
@sinxccc

新手学编程勿怪。学习的教程是:Learn Python The Hard Way

查了下,Emacs 是一种编辑器。我现在是在 gedit 里编辑,然后终端运行 python ex1.py
然后发现中文会报错,后来就了解到声明 utf-8 编码就可以了。
发现在其他人写的一些代码也发现有这样的声明,于是就对这个声明有了点疑问。
notedit
2013-07-16 02:13:50 +08:00
这个是python的规范 不是给emacs看的
python的解释器会读取这个设置
013231
2013-07-16 03:26:18 +08:00
Python支持3種不同方式的文件編碼聲明:
http://www.python.org/dev/peps/pep-0263/

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=<encoding name>

or (using formats recognized by popular editors)

#!/usr/bin/python
# -*- coding: <encoding name> -*-

or

#!/usr/bin/python
# vim: set fileencoding=<encoding name> :

More precisely, the first or second line must match the regular
expression "coding[:=]\s*([-\w.]+)". 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.

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.
reus
2013-07-16 05:20:10 +08:00
解释器可以理解多种格式的声明,写成这样是顺便给编辑器看。我不用emacs所以只用最短的那种
wildog
2013-07-16 05:59:54 +08:00
没有人觉得这种声明格式很呆萌吗
thedevil5032
2013-07-16 07:18:24 +08:00
这种申明只在 Python 2 中使用.
lisposter
2013-07-16 12:39:59 +08:00
我以为是卖萌来着。。。弱弱溜走
davepkxxx
2013-07-16 12:49:45 +08:00
颜文字?
xingzhi
2013-07-16 17:59:27 +08:00
@thedevil5032 python3 只允许 #coding:utf-8 这样的形式?
thedevil5032
2013-07-16 19:29:11 +08:00
@xingzhi Python3 不需要这样的东西, 因为 Python3 的 UTF8支持. 如果我没记错的话.
timonwong
2013-07-16 20:08:14 +08:00
@thedevil5032
@xingzhi
是可选,因为python3源文件默认编码是utf-8: http://www.python.org/dev/peps/pep-3120/
如果源文件需要是由其它编码保存的话,仍然需要指定encoding

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/75894

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX