python获取一组汉字拼音首字母的方法
作者:不吃皮蛋 发布时间:2023-04-08 16:43:02
标签:python,汉字,拼音
本文实例讲述了python获取一组汉字拼音首字母的方法。分享给大家供大家参考。具体实现方法如下:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def multi_get_letter(str_input):
if isinstance(str_input, unicode):
unicode_str = str_input
else:
try:
unicode_str = str_input.decode('utf8')
except:
try:
unicode_str = str_input.decode('gbk')
except:
print 'unknown coding'
return
return_list = []
for one_unicode in unicode_str:
return_list.append(single_get_first(one_unicode))
return return_list
def single_get_first(unicode1):
str1 = unicode1.encode('gbk')
try:
ord(str1)
return str1
except:
asc = ord(str1[0]) * 256 + ord(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 main(str_input):
a = multi_get_letter(str_input)
b = ''
for i in a:
b= b+i
print b
if __name__ == "__main__":
str_input=u'欢迎你'
main(str_input)
希望本文所述对大家的Python程序设计有所帮助。
0
投稿
猜你喜欢
- 将wav转amr,并转换成hex数组将wav文件快速转为amr,同时将arm文件转为16进制数组,保存在对应.h文件,供嵌入式设备使用(无文
- unittest是python的一个单元测试框架关于断言它是用于对一个确定结果和预测结果的一种判断,如果结果正确无任何返回效果,如果结果错误
- 由于本人使用的是windows 10 操作系统,所以介绍在 windows 10 系统中安装 Anaconda3 的过程。下载Anacond
- 运行环境: CentOS6.5_x64Python版本 : 2.6使用pyinstaller打包pyinstaller可以将python程序
- 1. 基本介绍tensorflow设备内存管理模块实现了一个best-fit with coalescing算法(后文简称bfc算法)。bf
- 本文实例为大家分享了PHP实现统计代码行数小工具,供大家参考,具体内容如下为了方面统计编程代码行数,做了一个小工具。自动统计指定目录以及目录
- 代码实例:try: import termios, TERMIOS 1except ImportErro
- 最近学习测试mybatis,单个增删改查都没问题,最后使用mvn test的时候发现了几个问题:1.update失败,原因是数据
- 举一个例子: <script> ~function() { alert("hello, world.");
- 取自ThinkPHP的文件缓存类代码,这里就不多废话了,小伙伴们自己看注释吧。<?php /** * @de
- 本文实例为大家分享了基于numpy实现逻辑回归的具体代码,供大家参考,具体内容如下交叉熵损失函数;sigmoid激励函数基于numpy的逻辑
- 太多程序员没有太多心思去关注他们每天都在面对的编程字体,然后编码工作需要长时间盯着屏幕并阅读一些非常复杂的文本。一个好的字体可以很大程度上提
- 一、数据库是什么?数据库是管理数据的一类软件。对数据的管理体现在两个方面,第一是描述数据,即一条数据如何表示,第二是组织数据,即很多数据如何
- 虽然网上有很多python开发环境搭建的文章,不过重复造轮子还是要的,记录一下过程,方便自己以后配置,也方便正在学习中的同事配置他们的环境。
- 适合各种浏览器的js拖动层,ie,firefox等,调用方便!<!DOCTYPE HTML PUBLIC "-//W3C//
- 为啥要写这个脚本五一前的准备下班的时候,看到同事为了做数据库的某个表的数据字典,在做一个复杂的人工操作,就是一个字段一个字段的纯手撸,那速度
- 导入模块模块通常为单独的.py文件,可以用import直接引用,可以作为模块的文件类型有.py、.pyo、.pyc、.pyd、.so、.dl
- Blackfriday是在Go中实现的Markdown处理器。您可以安全地输入用户提供的数据,速度快,支持通用扩展(表,智能标点符号替换等)
- 通常来说,Python的变量/数据类型非常多,但是它是不需要用户指定的,因为有些是根据部份系统函数生成,另外一些是自动根据变量的值识别的,这
- 背景:pytest以特定规则搜索测试用例,所以测试用例文件、测试类以及类中的方法、测试函数这些命名都必须符合规则,才能被pytest搜索到并