參見(jiàn):http://www.pythonclub.org/python-basic/codec
主要介紹了python的編碼機(jī)制,unicode, utf-8, utf-16, GBK, GB2312,ISO-8859-1 等編碼之間的轉(zhuǎn)換。
常見(jiàn)的編碼轉(zhuǎn)換分為以下幾種情況:
1.自動(dòng)識(shí)別字符串編碼:
#coding:utf8
#chartdet官方下載網(wǎng)站http://pypi.python.org/pypi/chardet

import urllib
import chardet

rawdata = urllib.urlopen('http://www.google.cn/').read()
print chardet.detect(rawdata)

輸出:
#confidence是可信度,encoding是編碼
{'confidence': 0.99, 'encoding': 'utf-8'}
2.unicode轉(zhuǎn)換為其他編碼
#coding:utf8

a = u'中文'
a_gb2312 = a.encode('gb2312')
print a_gb2312

輸出:
中文
3.其他編碼轉(zhuǎn)換為unicode
#coding:utf8

a = u'中文'
a_gb2312 = a.encode('gb2312')
print a_gb2312

#a為gb2312編碼,要轉(zhuǎn)為unicode. unicode(a, 'gb2312')或a.decode('gb2312')
print [unicode(a_gb2312,'gb2312')]
print [a_gb2312.decode('gb2312')]
輸出:
中文
[u'\u4e2d\u6587']
[u'\u4e2d\u6587']
4.非unicode編碼之間的相互轉(zhuǎn)化
#coding:utf8

a = u'中文'
a_gb2312 = a.encode('gb2312')
print a_gb2312

#編碼1轉(zhuǎn)換為編碼2可以先轉(zhuǎn)為unicode再轉(zhuǎn)為編碼2
a_unicode = a_gb2312.decode('gb2312')
print [a_unicode]
a_utf8 = a_unicode.encode('utf8')

#dos不識(shí)別utf8編碼,直接輸出會(huì)是亂碼
print [a_utf8]
輸出:
中文
[u'\u4e2d\u6587']
['\xe4\xb8\xad\xe6\x96\x87']
5.判斷字符串編碼
#coding:utf8

#isinstance(s, str) 用來(lái)判斷是否為一般字符串
#isinstance(s, unicode) 用來(lái)判斷是否為unicode 3
#如果一個(gè)字符串已經(jīng)是unicode了,再執(zhí)行unicode轉(zhuǎn)換有時(shí)會(huì)出錯(cuò)(并不都出錯(cuò))

def u(s,encoding):
if isinstance(s,unicode):
return s
else:
return unicode(s,encoding)
6.漢字轉(zhuǎn)化為unicode編碼
#coding:utf8

#該方法沒(méi)看懂,先留下了
name = '中國(guó)'
name = name.decode('utf8')
print name
tmpname = ""

for c in name:
c = "%%u%04X" % ord(c)
tmpname += c

print tmpname

輸出結(jié)果:
中國(guó)
%u4E2D%u56FD