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中常用的各种数据库操作模块和连接实例
May 29 Python
python实现上传样本到virustotal并查询扫描信息的方法
Oct 05 Python
Django1.7+python 2.78+pycharm配置mysql数据库教程
Nov 18 Python
解析Python编程中的包结构
Oct 25 Python
python re模块findall()函数实例解析
Jan 19 Python
python psutil库安装教程
Mar 19 Python
Python中使用遍历在列表中添加字典遇到的坑
Feb 27 Python
基于python调用psutil模块过程解析
Dec 20 Python
关于ZeroMQ 三种模式python3实现方式
Dec 23 Python
Python 批量读取文件中指定字符的实现
Mar 06 Python
一行代码python实现文件共享服务器
Apr 22 Python
pytorch 6 batch_train 批训练操作
May 28 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
PHP 飞信好友免费短信API接口开源版
2010/07/22 PHP
php提示无法加载或mcrypt没有找到 PHP 扩展 mbstring解决办法
2012/03/27 PHP
php获取访问者IP地址汇总
2015/04/24 PHP
Laravel框架路由和控制器的绑定操作方法
2018/06/12 PHP
ThinkPHP5.0框架结合Swoole开发实现WebSocket在线聊天案例详解
2019/04/02 PHP
jquery 页面全选框实践代码
2010/04/02 Javascript
JSON.parse 解析字符串出错的解决方法
2010/07/08 Javascript
jcarousellite.js 基于Jquery的图片无缝滚动插件
2010/12/30 Javascript
JavaScript中this关键词的使用技巧、工作原理以及注意事项
2014/05/20 Javascript
简单了解Backbone.js的Model模型以及View视图的源码
2016/02/14 Javascript
浅谈javascript中关于日期和时间的基础知识
2016/07/13 Javascript
基于NodeJS+MongoDB+AngularJS+Bootstrap开发书店案例分析
2017/01/12 NodeJs
node+express+ejs使用模版引擎做的一个示例demo
2017/09/18 Javascript
Node.js 实现远程桌面监控的方法步骤
2019/07/02 Javascript
Node.js开发之套接字(socket)编程入门示例
2019/11/05 Javascript
在Vue中使用Echarts可视化库的完整步骤记录
2020/11/18 Vue.js
python 环境变量和import模块导入方法(详解)
2017/07/11 Python
Python实现模拟登录网易邮箱的方法示例
2018/07/05 Python
Python中几种属性访问的区别与用法详解
2018/10/10 Python
OpenCV图像颜色反转算法详解
2019/05/13 Python
浅析PyTorch中nn.Linear的使用
2019/08/18 Python
解决pytorch GPU 计算过程中出现内存耗尽的问题
2019/08/19 Python
pandas 缺失值与空值处理的实现方法
2019/10/12 Python
给Python学习者的文件读写指南(含基础与进阶)
2020/01/29 Python
基于pygame实现童年掌机打砖块游戏
2020/02/25 Python
通俗讲解python 装饰器
2020/09/07 Python
python 线程的五个状态
2020/09/22 Python
如何通过Python实现RabbitMQ延迟队列
2020/11/28 Python
总结html5自定义属性有哪些
2020/04/01 HTML / CSS
经典c++面试题六
2012/01/18 面试题
5.12护士节演讲稿
2014/04/30 职场文书
机关班子查摆问题及整改措施
2014/10/28 职场文书
2015年街道办事处工作总结
2015/05/22 职场文书
春风化雨观后感
2015/06/11 职场文书
读完《骆驼祥子》的观后感!
2019/07/05 职场文书
Python中的tkinter库简单案例详解
2022/01/22 Python