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 20 Python
pycharm 使用心得(六)进行简单的数据库管理
Jun 06 Python
pandas带有重复索引操作方法
Jun 08 Python
python处理csv中的空值方法
Jun 22 Python
Python中asyncio模块的深入讲解
Jun 10 Python
Django 实现Admin自动填充当前用户的示例代码
Nov 18 Python
pycharm运行scrapy过程图解
Nov 22 Python
Python面向对象之多态原理与用法案例分析
Dec 30 Python
Python super()方法原理详解
Mar 31 Python
Python如何实现后端自定义认证并实现多条件登陆
Jun 22 Python
pytorch  网络参数 weight bias 初始化详解
Jun 24 Python
Python基础之Socket通信原理
Apr 22 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/01 无线电
PHP的宝库目录--PEAR
2006/10/09 PHP
PHP 实例化类的一点摘记
2008/03/23 PHP
php接口与接口引用的深入解析
2013/08/09 PHP
PHP中比较两个字符串找出第一个不同字符位置例子
2014/04/08 PHP
Jquery ajax不能解析json对象,报Invalid JSON错误的原因和解决方法
2010/03/27 Javascript
js函数中onmousedown和onclick的区别和联系探讨
2013/05/19 Javascript
一个JavaScript函数把URL参数解析成Json对象
2014/09/24 Javascript
jQuery实现单击弹出Div层窗口效果(可关闭可拖动)
2015/09/19 Javascript
js中unicode转码方法详解
2015/10/09 Javascript
jquery判断当前浏览器的实现代码
2015/11/07 Javascript
基于jQuery日历插件制作日历
2016/03/11 Javascript
Angularjs的ng-repeat中去除重复数据的方法
2016/08/05 Javascript
BootStrap中按钮点击后被禁用按钮的最佳实现方法
2016/09/23 Javascript
javascript高级模块化require.js的具体使用方法
2017/10/31 Javascript
原生js调用json方法总结
2018/02/22 Javascript
vue实现动态列表点击各行换色的方法
2018/09/13 Javascript
微信小程序开发实现的IP地址查询功能示例
2019/03/28 Javascript
浅谈js闭包理解
2019/04/01 Javascript
一个手写的vue放大镜效果
2019/08/09 Javascript
vue实现购物车结算功能
2020/06/18 Javascript
微信小程序换肤功能实现代码(思路详解)
2020/08/25 Javascript
js实现移动端图片滑块验证功能
2020/09/29 Javascript
微信小程序抽奖组件的使用步骤
2021/01/11 Javascript
python将图片文件转换成base64编码的方法
2015/03/14 Python
Python标准库之Sys模块使用详解
2015/05/23 Python
Django自定义插件实现网站登录验证码功能
2017/04/19 Python
python将一组数分成每3个一组的实例
2018/11/14 Python
详解python实现交叉验证法与留出法
2019/07/11 Python
python学习笔记之多进程
2020/08/06 Python
TensorFlow低版本代码自动升级为1.0版本
2021/02/20 Python
python如何发送带有附件、正文为HTML的邮件
2021/02/27 Python
韩国家庭购物网上商店:Nsmall
2017/05/07 全球购物
教师评优事迹材料
2014/01/10 职场文书
解除租房协议书
2014/12/03 职场文书
护理专业毕业自我鉴定
2019/08/12 职场文书