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创建系统目录的方法
Mar 11 Python
python利用高阶函数实现剪枝函数
Mar 20 Python
Python Unittest自动化单元测试框架详解
Apr 04 Python
Python中安装easy_install的方法
Nov 18 Python
Python matplotlib的使用并自定义colormap的方法
Dec 13 Python
python实现维吉尼亚算法
Mar 20 Python
django Admin文档生成器使用详解
Jul 22 Python
Python pandas实现excel工作表合并功能详解
Aug 29 Python
Django继承自带user表并重写的例子
Nov 18 Python
如何基于pythonnet调用halcon脚本
Jan 20 Python
Transpose 数组行列转置的限制方式
Feb 11 Python
如何编写python的daemon程序
Jan 07 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执行速度全攻略
2006/10/09 PHP
mysql5写入和读出乱码解决
2006/11/25 PHP
Jquery实现列表(隔行换色,全选,鼠标滑过当前行)效果实例
2013/06/09 Javascript
js 调用父窗口的具体实现代码
2013/07/15 Javascript
ExtJS[Desktop]实现图标换行示例代码
2013/11/17 Javascript
jquery mobile动态添加元素之后不能正确渲染解决方法说明
2014/03/05 Javascript
html的DOM中Event对象onabort事件用法实例
2015/01/21 Javascript
浅析JS异步加载进度条
2016/05/05 Javascript
JS读写CSS样式的方法汇总
2016/08/16 Javascript
js实现非常棒的弹出div
2016/10/06 Javascript
js事件源window.event.srcElement兼容性写法(详解)
2016/11/25 Javascript
详解vuex的简单使用
2018/03/12 Javascript
js中apply和Math.max()函数的问题及区别介绍
2018/03/27 Javascript
Canvas实现微信红包照片效果
2018/08/21 Javascript
vue动态设置路由权限的主要思路
2021/01/13 Vue.js
JS中锚点链接点击平滑滚动并自由调整到顶部位置
2021/02/06 Javascript
[02:41]DOTA2英雄基础教程 谜团
2013/12/10 DOTA
Python类的多重继承问题深入分析
2014/11/09 Python
Python正则表达式知识汇总
2017/09/22 Python
python获取文件路径、文件名、后缀名的实例
2018/04/23 Python
tensorflow 使用flags定义命令行参数的方法
2018/04/23 Python
python hashlib加密实现代码
2019/10/17 Python
python 基于PYMYSQL使用MYSQL数据库
2020/12/24 Python
Canvas中设置width与height的问题浅析
2018/11/01 HTML / CSS
naturalizer加拿大官网:美国娜然女鞋
2017/04/04 全球购物
波兰最大的度假胜地和城市公寓租赁运营商:Sun & Snow
2018/10/18 全球购物
DOUGLAS荷兰:购买香水和化妆品
2020/10/24 全球购物
教师党员个人整改措施
2014/10/27 职场文书
2014年工会工作总结
2014/11/12 职场文书
国王的演讲观后感
2015/06/03 职场文书
鲁滨逊漂流记读书笔记
2015/06/26 职场文书
好段摘抄大全(48句)
2019/08/08 职场文书
JVM上高性能数据格式库包Apache Arrow入门和架构详解(Gkatziouras)
2021/05/26 Servers
缓存替换策略及应用(以Redis、InnoDB为例)
2021/07/25 Redis
浅谈Redis的keys命令到底有多慢
2021/10/05 Redis
PYTHON使用Matplotlib去实现各种条形图的绘制
2022/03/22 Python