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中的应用之translate和maketrans用法详解
Aug 27 Python
Python编程中运用闭包时所需要注意的一些地方
May 02 Python
图文详解WinPE下安装Python
May 17 Python
windows下python连接oracle数据库
Jun 07 Python
Python操作MongoDB数据库的方法示例
Jan 04 Python
Python OOP类中的几种函数或方法总结
Feb 22 Python
pyinstaller打包opencv和numpy程序运行错误解决
Aug 16 Python
给ubuntu18安装python3.7的详细教程
Jun 08 Python
Java如何基于wsimport调用wcf接口
Jun 17 Python
深入浅析python 中的self和cls的区别
Jun 20 Python
如何利用Python动态模拟太阳系运转
Sep 04 Python
python Cartopy的基础使用详解
Nov 01 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
PHP APC配置文件2套和参数详解
2014/06/11 PHP
PHP递归获取目录内所有文件的实现方法
2016/11/01 PHP
PHP new static 和 new self详解
2017/02/19 PHP
php让json_encode不自动转义斜杠“/”的方法
2020/04/27 PHP
Javascript isArray 数组类型检测函数
2009/10/08 Javascript
JavaScript DOM元素尺寸和位置
2015/04/13 Javascript
学习javascript文件加载优化
2016/02/19 Javascript
javascript运算符——逻辑运算符全面解析
2016/06/27 Javascript
jQuery Ajax 实现分页 kkpager插件实例代码
2017/08/10 jQuery
angularjs实现的购物金额计算工具示例
2018/05/08 Javascript
vue项目移动端实现ip输入框问题
2019/03/19 Javascript
详解element-ui设置下拉选择切换必填和非必填
2019/06/17 Javascript
详解webpack打包vue项目之后生成的dist文件该怎么启动运行
2019/09/06 Javascript
javascript实现fetch请求返回的统一拦截
2019/12/22 Javascript
[36:54]Mineski vs Winstrike 2018国际邀请赛小组赛BO2 第一场 8.16
2018/08/17 DOTA
python遍历 truple list dictionary的几种方法总结
2016/09/11 Python
开源软件包和环境管理系统Anaconda的安装使用
2017/09/04 Python
python 实现视频流下载保存MP4的方法
2019/01/09 Python
解决安装pycharm后不能执行python脚本的问题
2019/01/19 Python
Python实现企业微信机器人每天定时发消息实例
2020/02/25 Python
Python 整行读取文本方法并去掉readlines换行\n操作
2020/09/03 Python
详解如何在pyqt中通过OpenCV实现对窗口的透视变换
2020/09/20 Python
鞋子女王塔玛拉·梅隆同名奢侈品牌:Tamara Mellon
2017/11/22 全球购物
Vichy薇姿加拿大官网:法国药妆,全球专业敏感肌护肤领先品牌
2018/07/11 全球购物
Ajax和javascript的区别
2013/07/20 面试题
大学生自助营养快餐店创业计划书
2014/01/13 职场文书
学雷锋演讲稿汇总
2014/05/10 职场文书
2014年英语教学工作总结
2014/12/17 职场文书
街道党风廉政建设调研报告
2015/01/01 职场文书
运动会表扬稿
2015/01/16 职场文书
营业员岗位职责
2015/02/11 职场文书
党员干部廉洁自律承诺书
2015/04/28 职场文书
好人好事新闻稿
2015/07/17 职场文书
2019七夕节祝福语36句,快来收藏吧
2019/08/06 职场文书
SpringBoot 整合mongoDB并自定义连接池的示例代码
2022/02/28 MongoDB
golang定时器
2022/04/14 Golang