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


Posted in Python onNovember 28, 2018

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实现的二维码生成小软件
Jul 11 Python
python下MySQLdb用法实例分析
Jun 08 Python
python过滤字符串中不属于指定集合中字符的类实例
Jun 30 Python
详解Django中的ifequal和ifnotequal标签使用
Jul 16 Python
详解python开发环境搭建
Dec 16 Python
TensorFlow实现非线性支持向量机的实现方法
Apr 28 Python
django的登录注册系统的示例代码
May 14 Python
python将数组n等分的实例
Dec 02 Python
Python decorator拦截器代码实例解析
Apr 04 Python
tensorflow 20:搭网络,导出模型,运行模型的实例
May 26 Python
如何利用python之wxpy模块玩转微信
Aug 17 Python
Python机器学习工具scikit-learn的使用笔记
Jan 28 Python
Python3爬虫使用Fidder实现APP爬取示例
Nov 27 #Python
python如何查看微信消息撤回
Nov 27 #Python
python中退出多层循环的方法
Nov 27 #Python
为什么Python中没有&quot;a++&quot;这种写法
Nov 27 #Python
django session完成状态保持的方法
Nov 27 #Python
Python3实现腾讯云OCR识别
Nov 27 #Python
python利用百度AI实现文字识别功能
Nov 27 #Python
You might like
Nginx下配置codeigniter框架方法
2015/04/07 PHP
PHP实现表单提交时去除斜杠的方法
2016/12/26 PHP
php使用fullcalendar日历插件详解
2019/03/06 PHP
Laravel框架Blade模板简介及模板继承用法分析
2019/12/03 PHP
Aster vs KG BO3 第一场2.19
2021/03/10 DOTA
javascript(jquery)利用函数修改全局变量的代码
2009/11/02 Javascript
jquery maxlength使用说明
2011/09/09 Javascript
面向对象的Javascript之二(接口实现介绍)
2012/01/27 Javascript
Raphael一个用于在网页中绘制矢量图形的Javascript库
2013/01/08 Javascript
获得Javascript对象属性个数的示例代码
2013/11/21 Javascript
jQuery Dialog 打开时自动聚焦的解决方法(两种方法)
2016/11/24 Javascript
js实现hashtable的赋值、取值、遍历操作实例详解
2016/12/25 Javascript
bootstrap confirmation按钮提示组件使用详解
2017/08/22 Javascript
vue使用中的内存泄漏【推荐】
2018/07/10 Javascript
Vue实现按钮旋转和移动位置的实例代码
2018/08/09 Javascript
JavaScript基于数组实现的栈与队列操作示例
2018/12/22 Javascript
微信小程序实现菜单左右联动
2020/05/19 Javascript
[20:21]《一刀刀一天》第十六期:TI国际邀请赛正式打响,总奖金超过550万
2014/05/23 DOTA
[03:23]我的刀塔你不可能这么可爱 第一期金萌萌的故事
2014/06/20 DOTA
[02:41]DOTA2亚洲邀请赛小组赛第三日 赛事回顾
2015/02/01 DOTA
[02:38]2018DOTA2亚洲邀请赛赛前采访-VGJ.T
2018/04/03 DOTA
Python 用户登录验证的小例子
2013/03/06 Python
浅谈python函数之作用域(python3.5)
2017/10/27 Python
Tesserocr库的正确安装方式
2018/10/19 Python
python 字典套字典或列表的示例
2019/12/16 Python
pytorch的batch normalize使用详解
2020/01/15 Python
Python自带的IDE在哪里
2020/07/01 Python
HTML5学习心得总结(推荐)
2016/07/08 HTML / CSS
NYX Professional Makeup俄罗斯官网:世界知名的化妆品品牌
2019/12/26 全球购物
毕业自我鉴定
2013/11/05 职场文书
高等教育专业自荐信范文
2014/03/26 职场文书
平安家庭事迹材料
2014/12/20 职场文书
行政复议决定书
2015/06/24 职场文书
七年级作文之冬景
2019/11/07 职场文书
gateway与spring-boot-starter-web冲突问题的解决
2021/07/16 Java/Android
使用Ajax实现进度条的绘制
2022/04/07 Javascript