Python 获取中文字拼音首个字母的方法

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 获取中文字拼音首个字母的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • 利用python实现汉字转拼音的2种方法

    前言 在浏览博客时,偶然看到了用python将汉字转为拼音的第三方包,但是在实现的过程中发现一些参数已经更新,现在将两种方法记录一下. xpinyin 在一些博客中看到,如果要转化成带音节的拼音,需要传递参数,'show_tone_marks=True',但我在实际使用时发现,已经没有这个参数了,变成了tone_marks,其它的参数和使用方法,一看就明白了,写的很清楚. 看下源码: class Pinyin(object): """translate chinese han

  • python实现将汉字转换成汉语拼音的库

    本文实例讲述了python实现将汉字转换成汉语拼音的库.分享给大家供大家参考.具体分析如下: 下面的这个python库可以很容易的将汉字转换成拼音,其中用到了一个word.data 的字典,可点击此处本站下载. #!/usr/bin/env python # -*- coding:utf-8 -*- __version__ = '0.9' __all__ = ["PinYin"] import os.path class PinYin(object): def __init__(sel

  • python去除拼音声调字母,替换为字母的方法

    第一种方法 import sys import unicodedata s = "Lǐ Zhōu Wú" remap = { # ord返回ascii值 ord('\t'): '', ord('\f'): '', ord('\r'): None } # 去除\t, \f, \r a = s.translate(remap) ''' 通过使用dict.fromkeys() 方法构造一个字典,每个Unicode 和音符作为键,对于的值全部为None 然后使用unicodedata.norm

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

  • Python 返回汉字的汉语拼音

    后来想到自己Delphi有一个获得拼音的代码.于是找了出来.研究了一下代码如下: 复制代码 代码如下: function get_hz_pywb(hzstr: string; pytype: integer): string; var I: Integer; allstr: string; hh: THandle; pp: pointer; ss: TStringList; function retturn_wbpy(tempstr: string; tqtype: integer): stri

  • Python 获取中文字拼音首个字母的方法

    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 <= -1

  • C# 获取汉字的拼音首字母

    获取汉字拼音的首字母是一个在做项目的过程中经常需要用到的功能,今天我们主要来探讨下C# 获取汉字的拼音首字母 /// <summary> /// 在指定的字符串列表CnStr中检索符合拼音索引字符串 /// </summary> /// <param name="CnStr">汉字字符串</param> /// <returns>相对应的汉语拼音首字母串</returns> public static string

  • C#获取汉字字符串拼音首字母的方法

    本文实例讲述了C#获取汉字字符串拼音首字母的方法.分享给大家供大家参考.具体如下: 这个C#类经常能够用到,将提取汉字的拼音首字母,方便用户查询 using System.Text; namespace DotNet.Utilities { public class PinYin { public string GetFirstLetter(string hz) { string ls_second_eng = "CJWGNSPGCGNESYPBTYYZDXYKYGTDJNNJQMBSGZSCY

  • python获取指定目录下所有文件名列表的方法

    本文实例讲述了python获取指定目录下所有文件名列表的方法.分享给大家供大家参考.具体实现方法如下: 这里python代码实现获取文件名列表的功能,可以指定文件中包含的字符,方便提取特定类型的文件名列表: # -*- coding: utf-8 -*- #~ #------------------------------------------------------------------ #~ module:wlab #~ Filename:wgetfilelist.py #~ Funct

  • Python获取电脑硬件信息及状态的实现方法

    本文以实例形式展示了Python获取电脑硬件信息及状态的实现方法,是Python程序设计中很有实用价值的技巧.分享给大家供大家参考之用.具体方法如下: 主要功能代码如下: #!/usr/bin/env python # encoding: utf-8 from optparse import OptionParser import os import re import json def main(): try: parser = OptionParser(usage="%prog [optio

  • Python获取二维矩阵每列最大值的方法

    因为做项目中间有一个很小的环节需要这个功能,所以就写了一个简单的小函数,下面是具体实现: #!usr/bin/env python #encoding:utf-8 ''' __Author__:沂水寒城 ''' def get_max_value(martix): ''' 得到矩阵中每一列最大的值 ''' res_list=[] for j in range(len(martix[0])): one_list=[] for i in range(len(martix)): one_list.ap

  • python pandas中DataFrame类型数据操作函数的方法

    python数据分析工具pandas中DataFrame和Series作为主要的数据结构. 本文主要是介绍如何对DataFrame数据进行操作并结合一个实例测试操作函数. 1)查看DataFrame数据及属性 df_obj = DataFrame() #创建DataFrame对象 df_obj.dtypes #查看各行的数据格式 df_obj['列名'].astype(int)#转换某列的数据类型 df_obj.head() #查看前几行的数据,默认前5行 df_obj.tail() #查看后几

  • python 获取毫秒数,计算调用时长的方法

    如题:在python的函数调用中需要记录时间,下面是记录毫秒时间的方法. import datetime import time t1 = datetime.datetime.now().microsecond t3 = time.mktime(datetime.datetime.now().timetuple() //这里调用你的方法 t2 = datetime.datetime.now().microsecond t4 = time.mktime(datetime.datetime.now(

  • Python 获取 datax 执行结果保存到数据库的方法

    执行 datax 作业,创建执行文件,在 crontab 中每天1点(下面有关系)执行: 其中 job_start 及 job_finish 这两行记录是自己添加的,为了方便识别出哪张表. #!/bin/bash source /etc/profile user1="root" pass1="pwd" user2="root" pass2="pwd" job_path="/opt/datax/job/" j

随机推荐