# -*- coding:utf-8 -*- 为什么要这样的格式? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
paicha

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

  •  
  •   paicha
    PRO
    paicha 2013 年 7 月 15 日 14210 次点击
    这是一个创建于 4667 天前的主题,其中的信息可能已经有所发展或是发生改变。
    「 -*- 」有什么意义么?

    同时发现这样写也是正常运行的:
    # coding:utf-8
    这样不是更简单么?
    17 条回复    1970-01-01 08:00:00 +08:00
    pubby
        1
    pubby  
       2013 年 7 月 15 日   1
    emacs会处理首行的这些“设置”信息

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

    格式就是在文件首行写上:
    -*- ......... -*-
    pubby
        2
    pubby  
       2013 年 7 月 15 日
    还有一种是写在文件末尾


    比如:
    /* ----- for emacs ----- */
    /* Local Variables: */
    /* mode: php */
    /* coding: utf-8-unix */
    /* tab-width: 4 */
    /* c-basic-offset: 4 */
    /* indent-tabs-mode: nil */
    /* End: */
    paicha
        3
    paicha  
    OP
    PRO
       2013 年 7 月 15 日
    @pubby 噢噢,那么用 # -*- coding:utf-8 -*- 可以理解为比 # coding:utf-8 兼容性更好么?
    keakon
        4
    keakon  
       2013 年 7 月 15 日
    那是 emacs 设置 encoding 的风格。
    http://www.python.org/dev/peps/pep-0263/
    pubby
        5
    pubby  
       2013 年 7 月 15 日
    sinxccc
        6
    sinxccc  
       2013 年 7 月 15 日
    这是给 Emacs 看的。
    paicha
        7
    paicha  
    OP
    PRO
       2013 年 7 月 15 日
    @pubby
    @sinxccc

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

    查了下,Emacs 是一种编辑器。我现在是在 gedit 里编辑,然后终端运行 python ex1.py
    然后发现中文会报错,后来就了解到声明 utf-8 编码就可以了。
    发现在其他人写的一些代码也发现有这样的声明,于是就对这个声明有了点疑问。
    notedit
        8
    notedit  
       2013 年 7 月 16 日   1
    这个是python的规范 不是给emacs看的
    python的解释器会读取这个设置
    013231
        9
    013231  
       2013 年 7 月 16 日   2
    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
        10
    reus  
       2013 年 7 月 16 日 via Android   1
    解释器可以理解多种格式的声明,写成这样是顺便给编辑器看。我不用emacs所以只用最短的那种
    wildog
        11
    wildog  
       2013 年 7 月 16 日 via Android   3
    没有人觉得这种声明格式很呆萌吗
    thedevil5032
        12
    thedevil5032  
       2013 年 7 月 16 日 via iPad
    这种申明只在 Python 2 中使用.
    lisposter
        13
    lisposter  
       2013 年 7 月 16 日
    我以为是卖萌来着。。。弱弱溜走
    davepkxxx
        14
    davepkxxx  
       2013 年 7 月 16 日
    颜文字?
    xingzhi
        15
    xingzhi  
       2013 年 7 月 16 日
    @thedevil5032 python3 只允许 #coding:utf-8 这样的形式?
    thedevil5032
        16
    thedevil5032  
       2013 年 7 月 16 日
    @xingzhi Python3 不需要这样的东西, 因为 Python3 的 UTF8支持. 如果我没记错的话.
    timonwong
        17
    timonwong  
       2013 年 7 月 16 日   2
    @thedevil5032
    @xingzhi
    是可选,因为python3源文件默认编码是utf-8: http://www.python.org/dev/peps/pep-3120/
    如果源文件需要是由其它编码保存的话,仍然需要指定encoding
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     866 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 59ms UTC 22:08 PVG 06:08 LAX 15:08 JFK 18:08
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86