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")

你可能感兴趣的:(c,python,Codec)