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多线程的退出控制实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-08-08
  • Python使用Flask调用API接口的方法

    Python使用Flask调用API接口的方法

    使用Python的Flask框架构建API接口是一种常见的做法,因为Flask轻量级且易于使用,本文给大家介绍了Python使用Flask调用API接口的方法,文中通过代码示例讲解的非常详细,需要的朋友可以参考下
    2024-05-05
  • Python自动化办公之定时发送邮件的实现

    Python自动化办公之定时发送邮件的实现

    python中的schedule模块可以使我们方便简单的使用定时任务,即在特定的时间自动的执行一些任务的功能,本文将用这一模块实现邮件自动发送,需要的可以参考一下
    2022-05-05
  • Pytest框架 conftest.py文件的使用详解

    Pytest框架 conftest.py文件的使用详解

    conftest.py是pytest特有的本地测试配置文件,既可以用来设置项目级别的fixture,也可以用来导入外部插件,本文给大家介绍Pytest框架 conftest.py文件的使用,感兴趣的朋友一起看看吧
    2022-06-06
  • Python虚拟环境安装及操作命令详解

    Python虚拟环境安装及操作命令详解

    本文主要介绍了Python虚拟环境安装及操作命令详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-07-07
  • Pyqt5 关于流式布局和滚动条的综合使用示例代码

    Pyqt5 关于流式布局和滚动条的综合使用示例代码

    这篇文章主要介绍了Pyqt5 关于流式布局和滚动条的综合使用示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-03-03
  • 一文教你掌握Python中Lambda表达式的5种实用技巧

    一文教你掌握Python中Lambda表达式的5种实用技巧

    在Python编程的宇宙里,有一个强大而灵活的工具经常被高效的程序员所利用——那就是Lambda表达式,下面就让我们深入了解Lambda表达式的妙用吧
    2024-01-01
  • pytorch 删除空权重模型文件夹的方法

    pytorch 删除空权重模型文件夹的方法

    如果文件夹exp开头的文件夹,里面没有pt pth模型文件,就把目录删掉,本文通过示例代码介绍pytorch 删除空权重模型文件夹的方法,感兴趣的朋友一起看看吧
    2023-11-11
  • python批量替换页眉页脚实例代码

    python批量替换页眉页脚实例代码

    这篇文章主要介绍了python批量替换页眉页脚实例代码,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
    2018-01-01
  • Python 判断奇数偶数的方法

    Python 判断奇数偶数的方法

    今天小编就为大家分享一篇Python 判断奇数偶数的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-12-12

最新评论