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

 更新时间:2015年07月01日 11:04:31   作者:不吃皮蛋  
这篇文章主要介绍了python获取一组汉字拼音首字母的方法,涉及Python针对汉字操作的相关技巧,需要的朋友可以参考下

本文实例讲述了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语音识别的转换方法

    语音识别技术,也被称为自动语音识别,目标是以电脑自动将人类的语音内容转换为相应的文字。应用包括语音拨号、语音导航、室内设备控制、语音文档检索、简单的听写数据录入等。本文给大家介绍python语音识别的方法,感兴趣的朋友一起看看吧
    2021-10-10
  • python中通过selenium简单操作及元素定位知识点总结

    python中通过selenium简单操作及元素定位知识点总结

    在本篇文章里小编给大家整理的是关于python中通过selenium简单操作及元素定位的知识点,有需要的朋友们可以学习下。
    2019-09-09
  • Django自定义排序ORM示例详解

    Django自定义排序ORM示例详解

    这篇文章主要为大家介绍了Django自定义排序ORM示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-09-09
  • Github Copilot结合python的使用方法详解

    Github Copilot结合python的使用方法详解

    最近也是听说github出了一种最新的插件叫做copilot,于是申请了,下面这篇文章主要给大家介绍了关于Github Copilot结合python使用的相关资料,需要的朋友可以参考下
    2022-04-04
  • Python字符串编码转换 encode()和decode()方法详细说明

    Python字符串编码转换 encode()和decode()方法详细说明

    这篇文章主要介绍了Python字符串编码转换 encode()和decode()方法详细的说明,下面文章围绕encode()和decode()方法的相相关资料展开内容,具有一定的价值,需要的朋友卡通参考一下
    2021-12-12
  • Python SELENIUM上传文件或图片实现过程

    Python SELENIUM上传文件或图片实现过程

    这篇文章主要介绍了Python SELENIUM上传文件或图片实现过程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-10-10
  • python正则表达式re模块详解

    python正则表达式re模块详解

    re 模块包含对正则表达式的支持,因为曾经系统学习过正则表达式,所以基础内容略过,直接看 python 对于正则表达式的支持。
    2014-06-06
  • 总结Python使用过程中的bug

    总结Python使用过程中的bug

    今天给大家带来的是关于Python的相关知识,文章围绕着Python使用过程中的bug展开,文中有非常详细的介绍,需要的朋友可以参考下
    2021-06-06
  • Python类型转换的魔术方法详解

    Python类型转换的魔术方法详解

    这篇文章主要介绍了Python类型转换的魔术方法详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-12-12
  • python3实现elasticsearch批量更新数据

    python3实现elasticsearch批量更新数据

    今天小编就为大家分享一篇python3实现elasticsearch批量更新数据,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-12-12

最新评论