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编程羊车门问题代码示例
Oct 25 Python
python实现发送邮件功能代码
Dec 14 Python
Python使用pickle模块存储数据报错解决示例代码
Jan 26 Python
python 将list转成字符串,中间用符号分隔的方法
Oct 23 Python
自学python的建议和周期预算
Jan 30 Python
Python叠加两幅栅格图像的实现方法
Jul 05 Python
python PyQt5/Pyside2 按钮右击菜单实例代码
Aug 17 Python
Pytorch 定义MyDatasets实现多通道分别输入不同数据方式
Jan 15 Python
Python中Selenium库使用教程详解
Jul 23 Python
用python实现前向分词最大匹配算法的示例代码
Aug 06 Python
python 获取字典键值对的实现
Nov 12 Python
python中_del_还原数据的方法
Dec 09 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
虹吸式咖啡探讨–研磨
2021/03/03 冲泡冲煮
Godaddy空间Zend Optimizer升级方法
2010/05/10 PHP
php读取图片内容并输出到浏览器的实现代码
2013/08/08 PHP
PHP防范SQL注入的具体方法详解(测试通过)
2014/05/09 PHP
php防止网站被攻击的应急代码
2015/10/21 PHP
[原创]PHP实现逐行删除文件右侧空格的方法
2015/12/25 PHP
jQuery实现分章节锚点“回到顶部”动画特效代码
2015/10/23 Javascript
iscroll碰到Select无法选择下拉刷新的解决办法
2016/05/21 Javascript
基于JavaScript FileReader上传图片显示本地链接
2016/05/27 Javascript
Javascript之Number对象介绍
2016/06/07 Javascript
使用vue编写一个点击数字计时小游戏
2016/08/31 Javascript
Vue中的ref作用详解(实现DOM的联动操作)
2017/08/21 Javascript
JS中offset和匀速动画详解
2018/02/06 Javascript
vue使用iframe嵌入网页的示例代码
2020/06/09 Javascript
vuejs+element UI点击编辑表格某一行时获取内容填入表单的示例
2018/10/31 Javascript
jquery中attr、prop、data区别与用法分析
2019/09/25 jQuery
Vue使用v-viewer实现图片预览
2020/10/21 Javascript
[00:47]TI7不朽珍藏III——沙王不朽展示
2017/07/15 DOTA
python实现电子词典
2020/04/23 Python
python中as用法实例分析
2015/04/30 Python
WINDOWS 同时安装 python2 python3 后 pip 错误的解决方法
2017/03/16 Python
Python smtplib实现发送邮件功能
2018/05/22 Python
Sanic框架流式传输操作示例
2018/07/18 Python
Python如何筛选序列中的元素的方法实现
2019/07/15 Python
实现Python与STM32通信方式
2019/12/18 Python
如何在Windows中安装多个python解释器
2020/06/16 Python
美国祛痘、抗衰老药妆品牌:Murad
2016/08/27 全球购物
英国最大的滑板品牌选择:Route One
2019/09/22 全球购物
乌克兰珠宝大卖场:Zlato.ua
2020/09/27 全球购物
物流仓管员岗位职责
2013/12/04 职场文书
新闻学专业求职信
2014/07/28 职场文书
委托书的格式
2014/08/01 职场文书
大学计划书范文800字
2014/08/14 职场文书
2015年药店店长工作总结
2015/04/29 职场文书
2015年小学辅导员工作总结
2015/05/27 职场文书
健康教育主题班会
2015/08/14 职场文书