应用场景之一:可用于获取名字首字母,在数据库中查询记录时,可以用它来排序输出。

from pytz import unicode

# 获取汉字首字母

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')

# print(len(str1))

try:

ord(str1)

return str1

except:

asc = str1[0] * 256 + str1[1] - 65536

# print(asc)

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):

list1 = multi_get_letter(str_input)

res = ''

for i in list1:

if type(i).__name__ =='bytes':

i = i.decode()

res = res+i

print(res)

if __name__ == "__main__":

str_input=u'世界欢迎你'

main(str_input)

输出:

sjhyn

【更新说明】

现在已经出现一个专门把汉子转拼音的库pypinyin,直接导入这个库使用就行了

pip install pypinyin

from pypinyin import pinyin, lazy_pinyin

def get_acronym(str_data):

"""

获取字符串的首字母

:param str_data: 字符串

:return: 字符串

"""

return "".join([i[0][0] for i in pinyin(str_data)])

if __name__ == '__main__':

print('拼音', lazy_pinyin('婺城区'))

print('拼音', pinyin('䆔'))

print("首字母", get_acronym('䆔心坡'))

输出:

拼音 ['wu', 'cheng', 'qu']

拼音 [['chōng']]

首字母 cxp

【后记】:如果文章对您有帮助,打赏下呗。微信 1257309054,欢迎交流学习*_*

微信

支付宝

python 获取首字母_python获取汉字首字母-Go语言中文社区相关推荐

  1. 用python绘制心形_python绘制心形图像-Go语言中文社区

    一.打印心形 print('n'.join([''.join([('Lovezhuzhu'[(x-y)%8]if((x*0.05)**2+(y*0.1)**2-1)**3-(x*0.05)**2*(y ...

  2. python小白社区_Python小白教学系列| 初识python-Go语言中文社区

    前言 关于Python的优缺点,用途,已在上一篇博客<浅谈C,JAVA,Python各自优劣>中介绍,这里不再过多赘述. 一.遇见Python Linux系统现在最好可以安装Python3 ...

  3. python爬取微博评论超100页_python爬取新浪微博评论-Go语言中文社区

    简介 爬取微博m站评论.由于api限制只能爬取前100页,如果想要更全数据需爬pc端. 工具 python 3.5 requests库 re库 步骤 1. 登陆保存cookie 首先,打开m.weib ...

  4. python语言中文社区-python numpy看这一篇就够了-Go语言中文社区

    numpy基础 1.为什么要学习numpy? -> 快速 -> 方便 -> 科学计算的基础库 2.什么是numpy? 一个在Python中做科学计算的基础库,重在数值计算,也是大部分 ...

  5. python读取nc文件并转换成csv_使用Python截取nc文件数据保存到CSV文件-Go语言中文社区...

    问题要求: 编写一个函数完成以下任务:截取经度在23°N-40°N,纬度在118°E-131°E范围内各属性不同深度的数据,使用Python中合适的数据结构将截取的数据保存到同名CSV文件中.(nc文 ...

  6. python基础语法大全turtle_Python 基础语法-turtle篇-Go语言中文社区

    Python 基础语法-turtle篇 今天这节课主要讲了类的概念,并引出turtle中的函数和Turtle类. -创建一个Turtle类:brad=turtle.Turtle() -定义Turtle ...

  7. 用python画路飞代码_用Python画路飞骷髅图案【附代码】-Go语言中文社区

    用Python代码实现这幅图的绘制,由于学艺不精,只能用直角坐标系作为参考画出来. 配色的RGB取色色块 还有重要的量角器兄弟 下面是代码 #路飞骷髅 import turtle as t #黄底帽子 ...

  8. python画一束花_用python画一朵玫瑰花,拿去表白趴-Go语言中文社区

    ''' Created on Nov 18, 2017 @author: QiZhao ''' import turtle # 设置初始位置 turtle.penup() turtle.left(90 ...

  9. python获取随机大小写字母_python获取随机大小写字母

    python如何自动生成单个随机字母(a-z). 1:mport random#导入random模块 用于生产随机数功能2:a = random.randint(97, 122)#利用random.r ...

最新文章

  1. zerodivisionerror什么意思python-【转】为什么 Python 中的 True 等于 1
  2. 论文笔记:Integrating Classification and Association Rule Mining (即,CBA算法介绍)
  3. 华为语音助手怎么关闭_一直觉得华为语音助手挺鸡肋的,直到用了这个功能!真香...
  4. 【数学建模】数模day13-灰色系统理论I-灰色关联与GM(1,1)预测
  5. [修正] 移动平台曲线不平滑的问题(如:TRectangle, TPath...等)
  6. java 如何秒速写一个线程启动
  7. 报错Uncaught ReferenceError: xxx is not defined at HTMLButtonElement.onclick
  8. silverlight数据绑定
  9. marathon传参一
  10. python机器学习库sklearn——决策树
  11. 动画算计算机专业吗,能够定义角色功能的计算机动画系统属于。 (1.0分)
  12. Incorrect string value: '\xF0\x9F\x98\x84\xF0\x9F 表情插入mysql 报错
  13. fatal error: GL/glut.h: 没有那个文件或目录
  14. 魔兽世界架设 mysql_魔兽世界单机版局域网的架设方法
  15. 信号与系统实验八 音频信号的时域、频域观测分析与图像的幅频相频重构
  16. C语言中 两个分号啥意思,问什么C程序里总是提示缺少分号;,而明明有分号?...
  17. python发微信图片加文字_python向企业微信发送文字和图片消息的示例
  18. ARM汇编之MOV指令
  19. 8051单片机的存储器结构
  20. 总结下我两个移动硬盘坏掉的经历...

热门文章

  1. 16 分频 32 分频是啥意思_Verilog 数字分频器的设计及验证
  2. 遗传算法系统辨识matlab程序,8.8 基于遗传算法的机械手参数辨识 系统辨识理论及Matlab仿真课件.ppt...
  3. Buuctf(pwn) ez_pz_hackover_2016 泄露栈地址,retshellcode;调试计算
  4. Python常用模块之random模块
  5. Python必备知识点:对Json的基本使用方法
  6. python3接收、解析邮件
  7. Numpy中stack(),hstack(),vstack()函数详解
  8. Python的 is 和 == 弄懂了吗?
  9. 在python中如何比较两个float类型的数据是否相等
  10. c语言fgetc()函数(从指定的流 stream 获取下一个字符(一个无符号字符),并把位置标识符往前移动)