PHP前端开发

python数字怎么转对应中文

百变鹏仔 1个月前 (01-22) #Python
文章标签 中文
python数字转对应中文的方法:1、通过“def num_to_char(num)...”方法将阿拉伯数字转换成中文;2、通过“def _to_chinese4(num)...”方法将数字转成对应中文即可。

本文操作环境:windows7系统,DELL G3电脑,python3.5版

python实现将阿拉伯数字转换成中文

第一种转换方式:

    1  -->  一    12   -->  一二def num_to_char(num):    """数字转中文"""    num=str(num)    new_str=""    num_dict={"0":u"零","1":u"一","2":u"二","3":u"三","4":u"四","5":u"五","6":u"六","7":u"七","8":u"八","9":u"九"}    listnum=list(num)    # print(listnum)    shu=[]    for i in listnum:        # print(num_dict[i])        shu.append(num_dict[i])    new_str="".join(shu)    # print(new_str)    return new_str

第二种转换方式:

立即学习“Python免费学习笔记(深入)”;

     1   --&gt;   一    12  --&gt;   十二    23  --&gt;  二十三_MAPPING = (u'零', u'一', u'二', u'三', u'四', u'五', u'六', u'七', u'八', u'九', u'十', u'十一', u'十二', u'十三', u'十四', u'十五', u'十六', u'十七',u'十八', u'十九')_P0 = (u'', u'十', u'百', u'千',)_S4 = 10 ** 4def _to_chinese4(num):    assert (0 = 10:            lst.append(num % 10)            num = num / 10        lst.append(num)        c = len(lst)  # 位数        result = u''        for idx, val in enumerate(lst):            val = int(val)            if val != 0:                result += _P0[idx] + _MAPPING[val]                if idx <p>推荐:《<a href="https://www.php.cn/course/list/30.html" target="_blank">python视频教程</a>》<br></p>