Chinaunix首页 | 论坛 | 博客
  • 博客访问: 181792
  • 博文数量: 79
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 15
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-25 14:40
文章分类
文章存档

2015年(1)

2014年(1)

2013年(2)

2012年(1)

2011年(5)

2010年(2)

2009年(6)

2008年(20)

2007年(27)

2006年(14)

我的朋友

分类: Python/Ruby

2007-07-06 14:09:18

  这几天试验了一下在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((i<<4)+j)
    print gbk3,
    #uni4=unicode(gbk3,'gbk')
    #print gbk3+': '+uni4+' ',
    print '\n'
    

阅读(2146) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~