欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python 獲取中文字拼音首個字母的方法

 更新時間:2018年11月28日 08:30:51   作者:HuangZhang_123  
今天小編就為大家分享一篇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('你好'))

運(yùn)行結(jié)果:

Python 中文字拼音首個字母

以上這篇Python 獲取中文字拼音首個字母的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python 多線程抓取圖片效率對比

    Python 多線程抓取圖片效率對比

    Python由于有全鎖局的存在,并不能利用多核優(yōu)勢。所以,如果你的多線程進(jìn)程是CPU密集型的,那多線程并不能帶來效率上的提升,相反還可能會因為線程的頻繁切換,導(dǎo)致效率下降;如果是IO密集型,多線程進(jìn)程可以利用IO阻塞等待時的空閑時間執(zhí)行其他線程,提升效率。
    2016-02-02
  • 基于Python測試程序是否有錯誤

    基于Python測試程序是否有錯誤

    這篇文章主要介紹了基于Python測試程序是否有錯誤,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-05-05
  • 使用Python批量壓縮tif文件操作步驟

    使用Python批量壓縮tif文件操作步驟

    Tif文件是柵格數(shù)據(jù)最常用的一種格式。圖像數(shù)據(jù)區(qū)以位圖的方式進(jìn)行數(shù)據(jù)的表示。因此Tif文件可以進(jìn)行壓縮,常用的壓縮方式有LZW、RAW、RLE、CCITT等
    2021-09-09
  • python實現(xiàn)bitmap數(shù)據(jù)結(jié)構(gòu)詳解

    python實現(xiàn)bitmap數(shù)據(jù)結(jié)構(gòu)詳解

    bitmap是很常用的數(shù)據(jù)結(jié)構(gòu),比如用于Bloom Filter中,下面是使用python實現(xiàn)bitmap數(shù)據(jù)結(jié)構(gòu)的代碼講解,需要的朋友可以參考下
    2014-02-02
  • Python中random模塊用法實例分析

    Python中random模塊用法實例分析

    這篇文章主要介紹了Python中random模塊用法,實例分析了Python中random模塊的使用技巧及字符串操作相關(guān)方法,需要的朋友可以參考下
    2015-05-05
  • Python中@符號的用法小結(jié)

    Python中@符號的用法小結(jié)

    @符號在Python中最常見的使用情況是在裝飾器中,本文主要介紹了Python中@符號的用法小結(jié),具有一定的參考價值,感興趣的可以了解一下
    2023-09-09
  • python通過鏈接抓取網(wǎng)站詳解

    python通過鏈接抓取網(wǎng)站詳解

    在本篇文章里小編給大家整理的是關(guān)于python通過鏈接抓取網(wǎng)站的詳細(xì)方法和知識點,需要的朋友們學(xué)習(xí)下。
    2019-11-11
  • pycharm打開長代碼文件CPU占用率過高的解決

    pycharm打開長代碼文件CPU占用率過高的解決

    這篇文章主要介紹了pycharm打開長代碼文件CPU占用率過高的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Python中pycharm編輯器界面風(fēng)格修改方法

    Python中pycharm編輯器界面風(fēng)格修改方法

    這篇文章主要介紹了Python中pycharm編輯器界面風(fēng)格修改方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03
  • Python實現(xiàn)Kerberos用戶的增刪改查操作

    Python實現(xiàn)Kerberos用戶的增刪改查操作

    這篇文章主要介紹了Python實現(xiàn)Kerberos用戶的增刪改查操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12

最新評論