看这个python脚本的运行结果,会加深你对python enc
2016-06-13 本文已影响72人
leoatchina
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