python数字怎么转对应中文


本文摘自php中文网,作者藏色散人,侵删。

python数字转对应中文的方法:1、通过“def num_to_char(num)...”方法将阿拉伯数字转换成中文;2、通过“def _to_chinese4(num)...”方法将数字转成对应中文即可。

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

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

第一种转换方式:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

    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

第二种转换方式:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

     1   -->   一

    12  -->   十二

    23  -->  二十三

_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 ** 4

def _to_chinese4(num):

    assert (0 <= num and num < _S4)

    if num < 20:

        return _MAPPING[num]

    else:

        lst = []

        while num >= 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 < c - 1 and lst[idx + 1] == 0:

                    result += u'零'

        return result[::-1]

推荐:《python视频教程》

以上就是python数字怎么转对应中文的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

分享一下Python数据分析常用的8款工具

Python用什么电脑

Python爬虫可以爬视频吗

如何改变 matplotlib 图像大小

怎么在Python安装pil

Python编程基础是什么

Python中协程实现tcp连接的代码示例

Python怎么创建模块

Python如何保留一位小数

Python的idle打不开怎么办

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...