python获取中文字符的拼音简写

该Python脚本提供了一个函数multi_get_letter,用于将输入的中文字符串转换为对应的拼音首字母。它首先尝试将输入解码为UTF-8,如果失败,则尝试解码为GBK。对于每个中文字符,它通过计算Unicode编码并根据预定义的范围映射到相应的英文首字母。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 
import os
import sys
#########start获取中文字符的拼音简写############
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+=single_get_first(one_unicode) 
    return return_list 
 
def single_get_first(unicode1): 
    str1 = unicode1.encode('gbk') 
    try:         
        ord(str1) 
        return str1 
    except: 
        asc = ord(str1[0]) * 256 + ord(str1[1]) - 65536 
        if asc >= -20319 and asc <= -20284: 
            return 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值