日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

發布時間:2025/3/19 python 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 获取首字母_python获取汉字首字母-Go语言中文社区 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

應用場景之一:可用于獲取名字首字母,在數據庫中查詢記錄時,可以用它來排序輸出。

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语言中文社区的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。