Python 获取中文字拼音首个字母的方法

 更新时间:2018年11月28日 08:30:51   作者:HuangZhang_123  
今天小编就为大家分享一篇Python 获取中文字拼音首个字母的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

Python:3.5

代码如下:

def single_get_first(unicode1):
 str1 = unicode1.encode('gbk')
 try:
 ord(str1)
 return str1.decode('gbk')
 except:
 asc = str1[0] * 256 + 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 getPinyin(string):
 if string == None:
 return None
 lst = list(string)
 charLst = []
 for l in lst:
 charLst.append(single_get_first(l))
 return ''.join(charLst)


if __name__ == '__main__':
 print(getPinyin('你好'))

运行结果:

Python 中文字拼音首个字母

以上这篇Python 获取中文字拼音首个字母的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Python中无限循环需要什么条件

    Python中无限循环需要什么条件

    在本篇文章里小编给大家分享的是关于Python中无限循环的条件的相关文章,需要的朋友们可以参考下。
    2020-05-05
  • python 操作excel表格的方法

    python 操作excel表格的方法

    这篇文章主要介绍了python 操作excel表格的方法,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下
    2020-12-12
  • 一文带你了解Python中的注释及变量

    一文带你了解Python中的注释及变量

    这篇文章主要给大家介绍了关于Python中注释及变量的相关资料,Python是一门动态类型的语言,因此无须提前声明变量类型,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2023-07-07
  • Python Numpy中数组的集合操作详解

    Python Numpy中数组的集合操作详解

    这篇文章主要为大家详细介绍了Python Numpy中数组的一些集合操作方法,文中的示例代码讲解详细,对我们学习Python有一定帮助,需要的可以参考一下
    2022-08-08
  • Python3使用正则表达式爬取内涵段子示例

    Python3使用正则表达式爬取内涵段子示例

    这篇文章主要介绍了Python3使用正则表达式爬取内涵段子,涉及Python正则匹配与文件读写相关操作技巧,需要的朋友可以参考下
    2018-04-04
  • python爬虫爬取指定内容的解决方法

    python爬虫爬取指定内容的解决方法

    这篇文章主要介绍了python爬虫爬取指定内容,爬取一些网站下指定的内容,一般来说可以用xpath来直接从网页上来获取,但是当我们获取的内容不唯一的时候我们无法选择,我们所需要的、所指定的内容,需要的朋友可以参考下
    2022-06-06
  • Python 实现训练集、测试集随机划分

    Python 实现训练集、测试集随机划分

    今天小编就为大家分享一篇Python 实现训练集、测试集随机划分,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-01-01
  • Python GUI编程完整示例

    Python GUI编程完整示例

    这篇文章主要介绍了Python GUI编程,结合完整示例形式分析了Python基于tkinter模块的GUI图形界面编程相关实现技巧,需要的朋友可以参考下
    2019-04-04
  • python里使用正则的findall函数的实例详解

    python里使用正则的findall函数的实例详解

    这篇文章主要介绍了python里使用正则的findall函数的实例详解的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
    2017-10-10
  • python中的json总结

    python中的json总结

    JSON(JavaScript Object Notation, JS 对象简谱) 是一种轻量级的数据交换格式。本文重点给大家介绍python中的json,感兴趣的朋友跟随小编一起看看吧
    2018-10-10

最新评论