python中文三言两语
这几天试验了一下在python中如何支持中文,下面这段简短的代码基本能让读者理解在python中如何处理中文,希望能对遇到同类问题的新手有用吧!
在winxp+python2.4下试验通过:
#test file for testing gkb unicode coding
#conver gbk to unicode
gbk1='我们' #a gbk string
print 'gbk sting: ',gbk1
uni1=unicode(gbk1,'gbk') #conver to unicode str
print 'uni sting: ',uni1
#form a string form gbk code or unicode
str1='\u6211\u4eec' #unicode code of us
uni2=unicode(str1,'unicode-escape')
print 'uni sting form uni code: ',uni2
str2='\xce\xd2\xc3\xc7' #gbkcode of us
uni3=unicode(str2,'gbk')
print 'uni sting form gbk code: ',uni3
#---------------------------------------
print "print unicode 0x6211 to 0x6221"
for i in range(0x6211,0x6221):
j='\u'+hex(i)[2:6]
k=unicode(j,'unicode-escape')
print j,': ',k
#-----------------------------------------
print "print c1xx area gbk code"
for i in range(0x4,0x10):
for j in range(0,0x10):
gbk3='\xc1'+chr((i4)+j)
print gbk3,
#uni4=unicode(gbk3,'gbk')
#print gbk3+': '+uni4+' ',
print '\n'