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爬取Coursera课程资源的详细过程
Nov 04 Python
pymongo实现控制mongodb中数字字段做加法的方法
Mar 26 Python
python爬虫之BeautifulSoup 使用select方法详解
Oct 23 Python
Python实现从log日志中提取ip的方法【正则提取】
Mar 31 Python
Python实现DDos攻击实例详解
Feb 02 Python
PyQt 实现使窗口中的元素跟随窗口大小的变化而变化
Jun 18 Python
python简单区块链模拟详解
Jul 03 Python
python 抓包保存为pcap文件并解析的实例
Jul 23 Python
Python Django 添加首页尾页上一页下一页代码实例
Aug 21 Python
如何基于Python创建目录文件夹
Dec 31 Python
python 项目目录结构设置
Feb 14 Python
python如何设置静态变量
Sep 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验证码(支持中文)
2007/02/14 PHP
PHP得到mssql的存储过程的输出参数功能实现
2012/11/23 PHP
部署PHP项目应该注意的几点事项分享
2013/12/20 PHP
PHP错误和异长常处理总结
2014/03/06 PHP
Yii2实现log输出到file及database的方法
2016/11/12 PHP
js移除事件 js绑定事件实例应用
2012/11/28 Javascript
jQuery实现密保互斥问题解决方案
2013/08/16 Javascript
javascript dom追加内容实现示例
2013/09/21 Javascript
JS记录用户登录次数实现代码
2014/01/15 Javascript
html的DOM中document对象images集合用法实例
2015/01/21 Javascript
JavaScript中document.forms[0]与getElementByName区别
2015/01/21 Javascript
JavaScript实现信用卡校验方法
2015/04/07 Javascript
javascript中BOM基础知识总结
2017/02/14 Javascript
jQuery EasyUI Accordion可伸缩面板组件使用详解
2017/02/28 Javascript
JavaScript脚本语言是什么_动力节点Java学院整理
2017/06/26 Javascript
JS运动特效之完美运动框架实例分析
2018/01/24 Javascript
用vuex写了一个购物车H5页面的示例代码
2018/12/04 Javascript
基于vue项目设置resolves.alias: '@'路径并适配webstorm
2020/12/02 Vue.js
nodejs+express最简易的连接数据库的方法
2020/12/23 NodeJs
js实现验证码干扰(动态)
2021/02/23 Javascript
使用 Python 获取 Linux 系统信息的代码
2014/07/13 Python
Python多线程编程(三):threading.Thread类的重要函数和方法
2015/04/05 Python
Python使用googletrans报错的解决方法
2018/09/25 Python
对python列表里的字典元素去重方法详解
2019/01/21 Python
python 实现生成均匀分布的点
2019/12/05 Python
Python动态声明变量赋值代码实例
2019/12/30 Python
Python使用20行代码实现微信聊天机器人
2020/06/05 Python
css3实现简单的白云飘动背景特效
2020/10/28 HTML / CSS
办公室保洁员岗位职责
2013/12/02 职场文书
20年同学聚会感言
2014/02/03 职场文书
学校安全责任书
2014/04/14 职场文书
企业员工集体活动方案
2014/08/17 职场文书
2014房屋登记授权委托书
2014/10/13 职场文书
齐云山导游词
2015/02/06 职场文书
2015个人简历自我评价语
2015/03/11 职场文书
教师节大会主持词
2015/07/06 职场文书