编码html 声明utf8:是coding:utf-8还是coding=urf-8

下次自动登录
现在的位置:
& 综合 & 正文
[Python]编码声明:是coding:utf-8还是coding=urf-8呢
我们知道在Python源码的头文件中要声明编码方式,如果你不只是会用到ascii码,很多人都写得都有点差别:
#coding=utf-8
#coding:utf-8
#-*- coding:utf-8 -*-
那么怎样写才是有效地呢,哪些优势无效的呢?
可以查看下http://www.python.org/dev/peps/pep-0263/的解释
粗略的看下:
这个PEP的目的是介绍在一个Python源文件中如何声明编码的语法。随后Python解释器会在解释文件的时候用到这些编码信息。最显著的是源文件中对Unicode的解释,使得在一个能识别Unicode的编辑器中使用如FUT-8编码成为可能
怎么声明呢?
如果在Python中我们并没有声明别的编码方式,就是以ASCII编码作为标准编码方式的
为了定义源文件的编码方式,一个魔法是的声明应当被放在这个文件的第一行或者是第二行例如:
#coding=&encoding name&
或者(使用流行编辑器中的格式化方式)
#!/usr/bin/python
# -*- coding: &encoding name& -*-
#!/usr/bin/python
# vim: set fileencoding=&encoding name& :
不管怎么样,这些在第一行或者第二行的声明都要符合正则表达式
"coding[:=]\s*([-\w.]+)"
所以我们就可以知道为什么使用冒号或者等号都可以了,如果声明的编码python不能识别就会报错
These are some examples to clarify the different styles for
defining the source code encoding at the top of a Python source
1. With interpreter binary and using Emacs style file encoding
#!/usr/bin/python
# -*- coding: latin-1 -*-
import os, sys
#!/usr/bin/python
# -*- coding: iso-8859-15 -*-
import os, sys
#!/usr/bin/python
# -*- coding: ascii -*-
import os, sys
2. Without interpreter line, using plain text:
# This Python file uses the following encoding: utf-8
import os, sys
3. Text editors might have different ways of defining the file's
encoding, e.g.
#!/usr/local/bin/python
# coding: latin-1
import os, sys
4. Without encoding comment, Python's parser will assume ASCII
#!/usr/local/bin/python
import os, sys
5. Encoding comments which don't work:
Missing "coding:" prefix:
#!/usr/local/bin/python
import os, sys
Encoding comment not on line 1 or 2:
#!/usr/local/bin/python
# -*- coding: latin-1 -*-
import os, sys
Unsupported encoding:
#!/usr/local/bin/python
# -*- coding: utf-42 -*-
import os, sys
以上几个例子充分说明了哪些是正确的写法,哪些是正确的写法
&&&&推荐文章:
【上篇】【下篇】python 支持3种编码声明,一般常用能见到下面两种
1.# -*- coding: utf-8 -*-
& 这种写法是为了兼容Emacs的编码声明
2.短一点,但Emacs不能用# coding=utf-8
短一点,但Emacs不能用
之所以要声明未编码类型 ,主要是中文出错的问题。
在python 文件开头(一般是第一行或第二行),用来说明你的Python源程序文件用使用的编码。缺省情况下你的程序需要使用ascii码来写,但如果在其中写中文的话,python解释器一般会报错,但如果加上你所用的文件编码,python就会自动处理不再报错。
这里要注意的是:
1.coding 后面使用 ":" 或 "=" 都可以
2.但是, ":" 或 "=" 必须和 coding之间没有空格。之前我就试过有空格声明失败,还是不支持中文。至于&":" 或 "=" 后面,有没有空格就没所谓了。
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& -*-
#!/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.
阅读(...) 评论()

我要回帖

更多关于 utf8声明 的文章

 

随机推荐