Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4436874
  • 博文数量: 1214
  • 博客积分: 13195
  • 博客等级: 上将
  • 技术积分: 9105
  • 用 户 组: 普通用户
  • 注册时间: 2007-01-19 14:41
个人简介

C++,python,热爱算法和机器学习

文章分类

全部博文(1214)

文章存档

2021年(13)

2020年(49)

2019年(14)

2018年(27)

2017年(69)

2016年(100)

2015年(106)

2014年(240)

2013年(5)

2012年(193)

2011年(155)

2010年(93)

2009年(62)

2008年(51)

2007年(37)

分类: IT业界

2016-08-06 09:26:37

原文地址:http://wangwei007.blog.51cto.com/68019/983289

最近需要写脚本,有个要取汉字拼音首字母的需求,上网查了一些材料,发现很容易实现,提出来大家分享。本脚本用于汉字的拼音的首个字母,如:”我是中国人“,得出的字母为:wszgr。


  1. #!/usr/bin/env python 
  2. # -*- coding: utf-8 -*- 
  3. def multi_get_letter(str_input): 
  4.      
  5.     if isinstance(str_input, unicode): 
  6.         unicode_str = str_input 
  7.     else
  8.         try
  9.             unicode_str = str_input.decode('utf8'
  10.         except
  11.             try
  12.                 unicode_str = str_input.decode('gbk'
  13.             except
  14.                 print 'unknown coding' 
  15.                 return 
  16.      
  17.     return_list = [] 
  18.     for one_unicode in unicode_str: 
  19.         return_list.append(single_get_first(one_unicode)) 
  20.     return return_list 
  21.  
  22. def single_get_first(unicode1): 
  23.     str1 = unicode1.encode('gbk'
  24.     try:         
  25.         ord(str1) 
  26.         return str1 
  27.     except
  28.         asc = ord(str1[0]) * 256 + ord(str1[1]) - 65536 
  29.         if asc >= -20319 and asc <= -20284
  30.             return 'a' 
  31.         if asc >= -20283 and asc <= -19776
  32.             return 'b' 
  33.         if asc >= -19775 and asc <= -19219
  34.             return 'c' 
  35.         if asc >= -19218 and asc <= -18711
  36.             return 'd' 
  37.         if asc >= -18710 and asc <= -18527
  38.             return 'e' 
  39.         if asc >= -18526 and asc <= -18240
  40.             return 'f' 
  41.         if asc >= -18239 and asc <= -17923
  42.             return 'g' 
  43.         if asc >= -17922 and asc <= -17418
  44.             return 'h' 
  45.         if asc >= -17417 and asc <= -16475
  46.             return 'j' 
  47.         if asc >= -16474 and asc <= -16213
  48.             return 'k' 
  49.         if asc >= -16212 and asc <= -15641
  50.             return 'l' 
  51.         if asc >= -15640 and asc <= -15166
  52.             return 'm' 
  53.         if asc >= -15165 and asc <= -14923
  54.             return 'n' 
  55.         if asc >= -14922 and asc <= -14915
  56.             return 'o' 
  57.         if asc >= -14914 and asc <= -14631
  58.             return 'p' 
  59.         if asc >= -14630 and asc <= -14150
  60.             return 'q' 
  61.         if asc >= -14149 and asc <= -14091
  62.             return 'r' 
  63.         if asc >= -14090 and asc <= -13119
  64.             return 's' 
  65.         if asc >= -13118 and asc <= -12839
  66.             return 't' 
  67.         if asc >= -12838 and asc <= -12557
  68.             return 'w' 
  69.         if asc >= -12556 and asc <= -11848
  70.             return 'x' 
  71.         if asc >= -11847 and asc <= -11056
  72.             return 'y' 
  73.         if asc >= -11055 and asc <= -10247
  74.             return 'z' 
  75.         return '' 
  76.  
  77. def main(str_input): 
  78.     a = multi_get_letter(str_input) 
  79.     b = '' 
  80.     for i in a: 
  81.         b= b+i 
  82.     print b 
  83. if __name__ == "__main__"
  84.     str_input='我是中国人' 
  85.     main(str_input) 

运行如下:

 


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