Python 獲取中文字拼音首個(gè)字母的方法
更新時(shí)間:2018年11月28日 08:30:51 作者:HuangZhang_123
今天小編就為大家分享一篇Python 獲取中文字拼音首個(gè)字母的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
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('你好'))
運(yùn)行結(jié)果:

以上這篇Python 獲取中文字拼音首個(gè)字母的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Python 中拼音庫(kù) PyPinyin 用法詳解
- python 批量將中文名轉(zhuǎn)換為拼音
- 基于Python第三方插件實(shí)現(xiàn)西游記章節(jié)標(biāo)注漢語(yǔ)拼音的方法
- 利用python實(shí)現(xiàn)漢字轉(zhuǎn)拼音的2種方法
- 用Python從0開(kāi)始實(shí)現(xiàn)一個(gè)中文拼音輸入法的思路詳解
- Python3實(shí)現(xiàn)漢語(yǔ)轉(zhuǎn)換為漢語(yǔ)拼音
- python去除拼音聲調(diào)字母,替換為字母的方法
- python獲取一組漢字拼音首字母的方法
- Python 返回漢字的漢語(yǔ)拼音
- Python實(shí)現(xiàn)拼音轉(zhuǎn)換
相關(guān)文章
python實(shí)現(xiàn)bitmap數(shù)據(jù)結(jié)構(gòu)詳解
bitmap是很常用的數(shù)據(jù)結(jié)構(gòu),比如用于Bloom Filter中,下面是使用python實(shí)現(xiàn)bitmap數(shù)據(jù)結(jié)構(gòu)的代碼講解,需要的朋友可以參考下2014-02-02
pycharm打開(kāi)長(zhǎng)代碼文件CPU占用率過(guò)高的解決
這篇文章主要介紹了pycharm打開(kāi)長(zhǎng)代碼文件CPU占用率過(guò)高的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
Python中pycharm編輯器界面風(fēng)格修改方法
這篇文章主要介紹了Python中pycharm編輯器界面風(fēng)格修改方法,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
Python實(shí)現(xiàn)Kerberos用戶的增刪改查操作
這篇文章主要介紹了Python實(shí)現(xiàn)Kerberos用戶的增刪改查操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12

