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解析nginx日志文件
May 11 Python
剖析Django中模版标签的解析与参数传递
Jul 21 Python
利用Python实现命令行版的火车票查看器
Aug 05 Python
从零开始学Python第八周:详解网络编程基础(socket)
Dec 14 Python
Python如何import文件夹下的文件(实现方法)
Jan 24 Python
Python cookbook(数据结构与算法)将名称映射到序列元素中的方法
Mar 22 Python
Python小进度条显示代码
Mar 05 Python
windows环境中利用celery实现简单任务队列过程解析
Nov 29 Python
python3 sleep 延时秒 毫秒实例
May 04 Python
Python json格式化打印实现过程解析
Jul 21 Python
python实战之90行代码写个猜数字游戏
Apr 22 Python
python神经网络Xception模型
May 06 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 文章采集正则代码
2009/12/28 PHP
PHP中将数组转成XML格式的实现代码
2011/08/08 PHP
利用curl 多线程 模拟 并发的详解
2013/06/14 PHP
PHP时间格式控制符对照表分享
2013/07/23 PHP
PHP PDOStatement::errorInfo讲解
2019/01/31 PHP
PHP tp5中使用原生sql查询代码实例
2020/10/28 PHP
js中判断控件是否存在
2010/08/25 Javascript
jQuery在vs2008及js文件中的无智能提示的解决方法
2010/12/30 Javascript
jQuery EasyUI API 中文文档 - MenuButton菜单按钮使用介绍
2011/10/06 Javascript
JS中批量给元素绑定事件过程中的相关问题使用闭包解决
2013/04/15 Javascript
addEventListener()第三个参数useCapture (Boolean)详细解析
2013/11/07 Javascript
浅谈javascript中call()、apply()、bind()的用法
2015/04/20 Javascript
jQuery中extend()和fn.extend()方法详解
2015/06/03 Javascript
Javascript vue.js表格分页,ajax异步加载数据
2016/10/24 Javascript
微信小程序 网络请求(post请求,get请求)
2017/01/17 Javascript
node+vue实现用户注册和头像上传的实例代码
2017/07/20 Javascript
浅谈ECMAScript6新特性之let、const
2017/08/02 Javascript
js实现canvas保存图片为png格式并下载到本地的方法
2017/08/31 Javascript
关于jQuery里prev()的简单操作代码
2017/10/27 jQuery
webpack4.x CommonJS模块化浅析
2018/11/09 Javascript
详解jQuery设置内容和属性
2019/04/11 jQuery
Nuxt v-bind绑定img src不显示的解决
2019/12/05 Javascript
Python检测网站链接是否已存在
2016/04/07 Python
在pycharm下设置自己的个性模版方法
2019/07/15 Python
python怎么提高计算速度
2020/06/11 Python
Selenium webdriver添加cookie实现过程详解
2020/08/12 Python
pycharm 添加解释器的方法步骤
2020/08/31 Python
HTML5 video视频字幕的使用和制作方法
2018/05/03 HTML / CSS
马来西亚排名第一的宠物用品店:Pets Wonderland
2020/04/16 全球购物
财务会计人员岗位职责
2013/11/30 职场文书
食品工程专业求职信
2014/06/15 职场文书
史上最全书信经典范文大全(建议收藏)
2019/07/10 职场文书
简单实现一个手持弹幕功能+文字抖动特效
2021/03/31 HTML / CSS
vue项目多环境配置(.env)的实现
2021/07/21 Vue.js
【海涛教你打DOTA】剑圣第一人称视角解说
2022/04/01 DOTA
Java中Dijkstra(迪杰斯特拉)算法
2022/05/20 Java/Android