python得到汉字拼音首字母

  1. #!/usr/bin/env python
  2. #coding=utf-8
  3. def get_Mutil_CnFirstLetter(str):
  4.     index = 0;
  5.     strReturnCn = ""
  6.     print "len(str)=%s" % len(str)
  7.     while index < (len(str)-1):
  8.         #print "strReturnCn=%s" % strReturnCn
  9.         #print get_cn_first_letter(str[index:index+2],"GBK")
  10.         #print str[index:index+2]
  11.         strReturnCn += get_cn_first_letter(str[index:index+2],"GBK")
  12.         index += 2;
  13.     return strReturnCn;
  14. def get_cn_first_letter(str,codec="UTF8"):
  15.     if codec!="GBK":
  16.         if codec!="unicode":
  17.             str=str.decode(codec)
  18.         str=str.encode("GBK")
  19.     
  20.     if str<"/xb0/xa1" or str>"/xd7/xf9":
  21.         return ""
  22.     if str<"/xb0/xc4":
  23.         return "a"
  24.     if str<"/xb2/xc0":
  25.         return "b"
  26.     if str<"/xb4/xed":
  27.         return "c"
  28.     if str<"/xb6/xe9":
  29.         return "d"
  30.     if str<"/xb7/xa1":
  31.         return "e"
  32.     if str<"/xb8/xc0":
  33.         return "f"
  34.     if str<"/xb9/xfd":
  35.         return "g"
  36.     if str<"/xbb/xf6":
  37.         return "h"
  38.     if str<"/xbf/xa5":
  39.         return "j"
  40.     if str<"/xc0/xab":
  41.         return "k"
  42.     if str<"/xc2/xe7":
  43.         return "l"
  44.     if str<"/xc4/xc2":
  45.         return "m"
  46.     if str<"/xc5/xb5":
  47.         return "n"
  48.     if str<"/xc5/xbd":
  49.         return "o"
  50.     if str<"/xc6/xd9":
  51.         return "p"
  52.     if str<"/xc8/xba":
  53.         return "q"
  54.     if str<"/xc8/xf5":
  55.         return "r"
  56.     if str<"/xcb/xf9":
  57.         return "s"
  58.     if str<"/xcd/xd9":
  59.         return "t"
  60.     if str<"/xce/xf3":
  61.         return "w"
  62.     if str<"/xd1/x88":
  63.         return "x"
  64.     if str<"/xd4/xd0":
  65.         return "y"
  66.     if str<"/xd7/xf9":
  67.         return "z"
  68. if __name__== '__main__':
  69.     print get_cn_first_letter("我""GBK")
  70.     print get_cn_first_letter("们""GBK")
  71.     print get_cn_first_letter("他""GBK")
  72.     print get_cn_first_letter("是""GBK")
  73.     print get_cn_first_letter("大""GBK")
  74.     print get_Mutil_CnFirstLetter("大埔的d")
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值