python获取一组汉字拼音首字母的方法


Posted in Python onJuly 01, 2015

本文实例讲述了python获取一组汉字拼音首字母的方法。分享给大家供大家参考。具体实现方法如下:

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 
def multi_get_letter(str_input): 
  if isinstance(str_input, unicode): 
    unicode_str = str_input 
  else: 
    try: 
      unicode_str = str_input.decode('utf8') 
    except: 
      try: 
        unicode_str = str_input.decode('gbk') 
      except: 
        print 'unknown coding' 
        return 
  return_list = [] 
  for one_unicode in unicode_str: 
    return_list.append(single_get_first(one_unicode)) 
  return return_list 
def single_get_first(unicode1): 
  str1 = unicode1.encode('gbk') 
  try:     
    ord(str1) 
    return str1 
  except: 
    asc = ord(str1[0]) * 256 + ord(str1[1]) - 65536 
    if asc >= -20319 and asc <= -20284: 
      return 'a' 
    if asc >= -20283 and asc <= -19776: 
      return 'b' 
    if asc >= -19775 and asc <= -19219: 
      return 'c' 
    if asc >= -19218 and asc <= -18711: 
      return 'd' 
    if asc >= -18710 and asc <= -18527: 
      return 'e' 
    if asc >= -18526 and asc <= -18240: 
      return 'f' 
    if asc >= -18239 and asc <= -17923: 
      return 'g' 
    if asc >= -17922 and asc <= -17418: 
      return 'h' 
    if asc >= -17417 and asc <= -16475: 
      return 'j' 
    if asc >= -16474 and asc <= -16213: 
      return 'k' 
    if asc >= -16212 and asc <= -15641: 
      return 'l' 
    if asc >= -15640 and asc <= -15166: 
      return 'm' 
    if asc >= -15165 and asc <= -14923: 
      return 'n' 
    if asc >= -14922 and asc <= -14915: 
      return 'o' 
    if asc >= -14914 and asc <= -14631: 
      return 'p' 
    if asc >= -14630 and asc <= -14150: 
      return 'q' 
    if asc >= -14149 and asc <= -14091: 
      return 'r' 
    if asc >= -14090 and asc <= -13119: 
      return 's' 
    if asc >= -13118 and asc <= -12839: 
      return 't' 
    if asc >= -12838 and asc <= -12557: 
      return 'w' 
    if asc >= -12556 and asc <= -11848: 
      return 'x' 
    if asc >= -11847 and asc <= -11056: 
      return 'y' 
    if asc >= -11055 and asc <= -10247: 
      return 'z' 
    return '' 
def main(str_input): 
  a = multi_get_letter(str_input) 
  b = '' 
  for i in a: 
    b= b+i 
  print b 
if __name__ == "__main__": 
  str_input=u'欢迎你' 
  main(str_input)

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
Python删除空文件和空文件夹的方法
Jul 14 Python
详解Python3 中hasattr()、getattr()、setattr()、delattr()函数及示例代码数
Apr 18 Python
python 实现得到当前时间偏移day天后的日期方法
Dec 31 Python
微信公众号token验证失败解决方案
Jul 22 Python
python正则表达式匹配不包含某几个字符的字符串方法
Jul 23 Python
Python数据存储之 h5py详解
Dec 26 Python
利用Tensorboard绘制网络识别准确率和loss曲线实例
Feb 15 Python
Python读取二进制文件代码方法解析
Jun 22 Python
使用Dajngo 通过代码添加xadmin用户和权限(组)
Jul 03 Python
Windows下pycharm安装第三方库失败(通用解决方案)
Sep 17 Python
matplotlib交互式数据光标实现(mplcursors)
Jan 13 Python
浅谈python数据类型及其操作
May 25 Python
python的keyword模块用法实例分析
Jun 30 #Python
Python实现监控程序执行时间并将其写入日志的方法
Jun 30 #Python
python实现爬取千万淘宝商品的方法
Jun 30 #Python
python简单判断序列是否为空的方法
Jun 30 #Python
python检查序列seq是否含有aset中项的方法
Jun 30 #Python
python判断一个集合是否包含了另外一个集合中所有项的方法
Jun 30 #Python
python过滤字符串中不属于指定集合中字符的类实例
Jun 30 #Python
You might like
header导出Excel应用示例
2014/01/24 PHP
Yii入门教程之目录结构、入口文件及路由设置
2014/11/25 PHP
symfony2.4的twig中date用法分析
2016/03/18 PHP
Laravel框架实现的使用smtp发送邮件功能示例
2019/03/12 PHP
IE浏览器PNG图片透明效果代码
2008/09/02 Javascript
Mootools 1.2教程 滑动效果(Slide)
2009/09/15 Javascript
在次封装easyui-Dialog插件实现代码
2010/11/14 Javascript
基于jQuery中对数组进行操作的方法
2013/04/16 Javascript
浅析JavaScript原型继承的陷阱
2013/12/03 Javascript
JavaScript中的值类型转换介绍
2014/12/31 Javascript
JavaScript中实现单体模式分享
2015/01/29 Javascript
学习javascript面向对象 理解javascript对象
2016/01/04 Javascript
基于Jquery插件实现跨域异步上传文件功能
2016/04/26 Javascript
gulp-htmlmin压缩html的gulp插件实例代码
2016/06/06 Javascript
js实现可键盘控制的简单抽奖程序
2016/07/13 Javascript
使用Require.js封装原生js轮播图的实现代码
2017/06/15 Javascript
基于canvas粒子系统的构建详解
2017/08/31 Javascript
JavaScript学习笔记之DOM操作实例分析
2019/01/08 Javascript
微信公众号平台接口开发 获取access_token过程解析
2019/08/14 Javascript
浅谈vue中$bus的使用和涉及到的问题
2020/07/28 Javascript
记一次vue跨域的解决
2020/10/21 Javascript
详解Python中映射类型的内建函数和工厂函数
2015/08/19 Python
基于python plotly交互式图表大全
2019/12/07 Python
CSS3中Color的一些特性介绍
2012/05/27 HTML / CSS
台湾线上百货零售购物平台:friDay购物
2017/08/18 全球购物
酒店员工职业生涯规划
2014/02/25 职场文书
经典婚礼主持词
2014/03/13 职场文书
毕业评语大全
2014/05/04 职场文书
反对四风自我剖析材料
2014/10/07 职场文书
教师工作表现自我评价
2015/03/05 职场文书
费用申请报告范文
2015/05/15 职场文书
学生会招新宣传语
2015/07/13 职场文书
html+css合并表格边框的示例代码
2021/03/31 HTML / CSS
详解TS数字分隔符和更严格的类属性检查
2021/05/06 Javascript
Android开发手册自定义Switch开关按钮控件
2022/06/10 Java/Android
使用CSS实现音波加载效果
2023/05/07 HTML / CSS