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中的异常处理简明介绍
Apr 13 Python
使用Python构建Hopfield网络的教程
Apr 14 Python
python实现简单的单变量线性回归方法
Nov 08 Python
Pytorch 实现权重初始化
Dec 31 Python
Tensorflow累加的实现案例
Feb 05 Python
Selenium元素定位的30种方式(史上最全)
May 11 Python
python中如何打包用户自定义模块
Sep 23 Python
Python 制作查询商品历史价格的小工具
Oct 20 Python
scrapy redis配置文件setting参数详解
Nov 18 Python
Python脚本调试工具安装过程
Jan 11 Python
教你怎么用Python处理excel实现自动化办公
Apr 30 Python
Python+Tkinter打造签名设计工具
Apr 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强制下载PDF文件示例
2014/01/17 PHP
php按字符无乱码截取中文的方法
2015/03/27 PHP
客户端限制只能上传jpg格式图片的js代码
2010/12/09 Javascript
javascript学习笔记(二十) 获得和设置元素的特性(属性)
2012/06/20 Javascript
javascript中直接写php代码的方法
2013/07/31 Javascript
node.js中的buffer.toJSON方法使用说明
2014/12/14 Javascript
node.js中的fs.realpathSync方法使用说明
2014/12/16 Javascript
轻量级javascript 框架Backbone使用指南
2015/07/24 Javascript
js实现带圆角的两级导航菜单效果代码
2015/08/24 Javascript
Angular和Vue双向数据绑定的实现原理(重点是vue的双向绑定)
2016/11/22 Javascript
Javascript之面向对象--接口
2016/12/02 Javascript
vue .js绑定checkbox并获取、改变选中状态的实例
2018/08/24 Javascript
vue实现双向绑定和依赖收集遇到的坑
2018/11/29 Javascript
CKeditor富文本编辑器使用技巧之添加自定义插件的方法
2019/06/14 Javascript
angularjs自定义过滤器demo示例
2019/08/24 Javascript
js 递归json树实现根据子id查父id的方法分析
2019/11/08 Javascript
详解JavaScript修改注册表的方法
2020/01/05 Javascript
详解vue中v-bind:style效果的自定义指令
2020/01/21 Javascript
jquery实现直播视频弹幕效果
2020/02/25 jQuery
Python模块学习 filecmp 文件比较
2012/08/27 Python
使用Python编写一个模仿CPU工作的程序
2015/04/16 Python
Python实现识别手写数字大纲
2018/01/29 Python
Python Logging 日志记录入门学习
2018/06/02 Python
Python get获取页面cookie代码实例
2018/09/12 Python
python实现海螺图片的方法示例
2019/05/12 Python
python requests证书问题解决
2019/09/05 Python
通过实例解析Python调用json模块
2019/12/11 Python
使用OpenCV对车道进行实时检测的实现示例代码
2020/06/19 Python
PyTorch预训练Bert模型的示例
2020/11/17 Python
python 使用paramiko模块进行封装,远程操作linux主机的示例代码
2020/12/03 Python
美国最顶级的精品店之一:Hampden Clothing
2016/12/22 全球购物
优质服务演讲稿
2014/05/14 职场文书
2015教师年度考核评语
2015/03/25 职场文书
2015年音乐教研组工作总结
2015/07/22 职场文书
thinkphp 获取控制器及控制器方法
2021/04/16 PHP
python 自动刷新网页的两种方法
2021/04/20 Python