このpythonスクリプトの実行結果を見ると、python encode,decodeの理解が深まります.
1431 ワード
import sys
import chardet
u=u'unicode '
s='str '
ueg=u.encode('gbk') # gbk
ueu=u.encode('utf-8')# utf-8
sdg=s.decode('gbk')
sdu=s.decode('utf-8')
print r'type(%s) is %s' %('u',type(u))
print r'type(%s) is %s' %('s',type(s))
print r'type(%s) is %s' %('ueg',type(ueg))
print r'type(%s) is %s' %('ueu',type(ueu))
print r'type(%s) is %s' %('sdg',type(sdg))
print r'type(%s) is %s' %('sdu',type(sdu))
print "----------------------------"
print r'chardet(%s) is %s' %('s',chardet.detect(s))
print r'chardet(%s) is %s' %('ueg',chardet.detect(ueg))
print r'chardet(%s) is %s' %('ueu',chardet.detect(ueu))
print "----------------------------"
print r'u encode utf-8:',u.encode('utf-8')
print r'u encode gbk:',u.encode('gbk')
print r'sdg encode utf-8:',sdg.encode('utf-8')
print r'sdg encode gbk:',sdg.encode('gbk')
print r'sdu encode utf-8:',sdu.encode('utf-8')
print r'sdu encode gbk:',sdu.encode('gbk')
print "----------------------------"
z=0
for i in [u,s,ueg,ueu,sdg,sdu]:
z=z+1
print z,
try:
print i
except Exception, e:
print e
continue