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 相关文章推荐
Python3读取文件常用方法实例分析
May 22 Python
python实现解数独程序代码
Apr 12 Python
python 读取目录下csv文件并绘制曲线v111的方法
Jul 06 Python
Python切片操作深入详解
Jul 27 Python
windows下python 3.6.4安装配置图文教程
Aug 21 Python
Python高级特性与几种函数的讲解
Mar 08 Python
Python对HTML转义字符进行反转义的实现方法
Apr 28 Python
python实现集中式的病毒扫描功能详解
Jul 09 Python
python读取.mat文件的数据及实例代码
Jul 12 Python
python 进程间数据共享multiProcess.Manger实现解析
Sep 23 Python
Python爬虫JSON及JSONPath运行原理详解
Jun 04 Python
用python写PDF转换器的实现
Oct 29 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里的JS打印函数
2006/10/09 PHP
令PHP初学者头疼十四条问题大总结
2008/11/12 PHP
php新浪微博登录接口用法实例
2014/12/23 PHP
php操作xml入门之xml标签的属性分析
2015/01/23 PHP
php提交表单发送邮件的方法
2015/03/20 PHP
tp5(thinkPHP5)框架连接数据库的方法示例
2018/12/24 PHP
Laravel-admin之修改操作日志的方法
2019/09/30 PHP
PHP如何通过带尾指针的链表实现'队列'
2020/10/22 PHP
对YUI扩展的Gird组件 Part-2
2007/03/10 Javascript
JavaScript 无符号右移赋值操作
2009/04/17 Javascript
微信JS接口汇总及使用详解
2015/01/09 Javascript
jquery自定义表格样式
2015/11/23 Javascript
浅析BootStrap栅格系统
2016/06/07 Javascript
JS查找字符串中出现次数最多的字符
2016/09/05 Javascript
详解vue-cli快速构建vue应用并实现webpack打包
2017/12/13 Javascript
如何利用vue+vue-router+elementUI实现简易通讯录
2019/05/13 Javascript
vue+element加入签名效果(移动端可用)
2019/06/17 Javascript
vue常用高阶函数及综合实例
2021/02/25 Vue.js
[06:59]DOTA2-DPC中国联赛3月7日Recap集锦
2021/03/11 DOTA
安装ElasticSearch搜索工具并配置Python驱动的方法
2015/12/22 Python
Python用Pillow(PIL)进行简单的图像操作方法
2017/07/07 Python
python 调用c语言函数的方法
2017/09/29 Python
用pandas按列合并两个文件的实例
2018/04/12 Python
Python json模块dumps、loads操作示例
2018/09/06 Python
200行python代码实现2048游戏
2019/07/17 Python
wxPython实现绘图小例子
2019/11/19 Python
解决python pandas读取excel中多个不同sheet表格存在的问题
2020/07/14 Python
html5 Canvas实现图片旋转的示例
2018/01/15 HTML / CSS
香港钟表珠宝首饰商城:OneMallTime网摩间
2016/10/14 全球购物
煤矿班组长竞聘书
2014/03/31 职场文书
韩语专业职业生涯规划范文:成功之路就在我们脚下
2014/09/11 职场文书
2014年村计划生育工作总结
2014/11/14 职场文书
.Net Core导入千万级数据至Mysql的步骤
2021/05/24 MySQL
js判断两个数组相等的5种方法
2022/05/06 Javascript
Django框架模板用法详解
2022/06/10 Python
MySQL解决Navicat设置默认字符串时的报错问题
2022/06/16 MySQL