python中的编码问题

1. 声明

首先声明几个名词解释:
1. 文件自己的编码:能够利用file检查文件的编码格式。例如:python

file test.py
test.py: UTF-8 Unicode Java program text

这时文件自己的编码是UTF-8的。vim

  1. python代码的编码:打开文件,在文件上部添加的encoding。例如:
# -*- encoding: utf-8 -*-
import sys

2. 怎么设定编码

既然存在2个编码,那么就存在相同和不一样状况,二者相同天然是没问题,好比都是gb18030或者utf-8,若是不一样会怎么样呢?显然是编码显示错误,看以下几个例子:
文件编码为utf-8,代码编码为gb18030,有:this

# -*- encoding: gb18030 -*-

str_alphabeta = "ABCDEFG"
print type(str_alphabeta)
print str_alphabeta

str_kanji = "可口可乐"
print type(str_kanji)
print str_kanji

输出为:google

File "test.py", line 1
SyntaxError: encoding problem: with BOM

出现一个新的关键词BOM,这个能够google一下,若是你在vim中看到<feff>这么一个东西,那也是BOM引发的,若是文档是utf-8我的以为使用无BOM格式会好处理点。
那么为了能正常运行,须要文档的编码和代码的编码一致编码

3. unicode_literals

来自future库的内容表示如今还在“试用”阶段,若是你追求“新”就用,若是你追求“稳”就别用(我这么理解的,虽然我常常用division)。
unicode_literals的帮助是这么写的:code

>>> help(unicode_literals)
Help on instance of _Feature in module __future__:

class _Feature
 |  Methods defined here:
 |  
 |  __init__(self, optionalRelease, mandatoryRelease, compiler_flag)
 |  
 |  __repr__(self)
 |  
 |  getMandatoryRelease(self)
 |      Return release in which this feature will become mandatory.
 |      
 |      This is a 5-tuple, of the same form as sys.version_info, or, if
 |      the feature was dropped, is None.
 |  
 |  getOptionalRelease(self)
 |      Return first release in which this feature was recognized.
 |      
 |      This is a 5-tuple, of the same form as sys.version_info.

简单地说就是,非unicode(32)的代码编码(例如utf-8),直接赋值一个字符串获得的编码是代码的编码方式,对象的类型是str,可是若是字符串前面加一个“u”就表示这个字符串是unicode(32)的编码,例如:orm

# -*- encoding: utf-8 -*-

str_kanji = "可口可乐"
print type(str_kanji)
print str_kanji

str_kanji_unicode = u"可口可乐"
print type(str_kanji_unicode)
print str_kanji_unicode

输出为:对象

<type 'str'>
可口可乐
<type 'unicode'>
可口可乐

第一个可口可乐是utf-8编码的(能够经过locale中的LC_CTYPE来验证),第二个是unicode(32)的。
若是import unicode_literals则变为(代码略):utf-8

<type 'unicode'>
可口可乐
<type 'unicode'>
可口可乐
相关文章
相关标签/搜索