python数字转对应中文的方法总结


Posted in Python onAugust 02, 2021

本文操作环境:

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

第二种转换方式:

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]

实例扩展:

#!/usr/bin/python
#-*- encoding: utf-8 -*-

import types

class NotIntegerError(Exception):
  pass

class OutOfRangeError(Exception):
  pass

_MAPPING = (u'零', u'一', u'二', u'三', u'四', u'五', u'六', u'七', u'八', u'九', )
_P0 = (u'', u'十', u'百', u'千', )
_S4, _S8, _S16 = 10 ** 4 , 10 ** 8, 10 ** 16
_MIN, _MAX = 0, 9999999999999999

def _to_chinese4(num):
  '''转换[0, 10000)之间的阿拉伯数字
  '''
  assert(0 <= num and num < _S4)
  if num < 10:
    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):
      if val != 0:
        result += _P0[idx] + _MAPPING[val]
        if idx < c - 1 and lst[idx + 1] == 0:
          result += u'零'
    
    return result[::-1].replace(u'一十', u'十')
    
def _to_chinese8(num):
  assert(num < _S8)
  to4 = _to_chinese4
  if num < _S4:
    return to4(num)
  else:
    mod = _S4
    high, low = num / mod, num % mod
    if low == 0:
      return to4(high) + u'万'
    else:
      if low < _S4 / 10:
        return to4(high) + u'万零' + to4(low)
      else:
        return to4(high) + u'万' + to4(low)
      
def _to_chinese16(num):
  assert(num < _S16)
  to8 = _to_chinese8
  mod = _S8
  high, low = num / mod, num % mod
  if low == 0:
    return to8(high) + u'亿'
  else:
    if low < _S8 / 10:
      return to8(high) + u'亿零' + to8(low)
    else:
      return to8(high) + u'亿' + to8(low)
    
def to_chinese(num):
  if type(num) != types.IntType and type(num) != types.LongType:
    raise NotIntegerError(u'%s is not a integer.' % num)
  if num < _MIN or num > _MAX:
    raise OutOfRangeError(u'%d out of range[%d, %d)' % (num, _MIN, _MAX))
  
  if num < _S4:
    return _to_chinese4(num)
  elif num < _S8:
    return _to_chinese8(num)
  else:
    return _to_chinese16(num)
  
if __name__ == '__main__':
  print to_chinese(9000)

以上就是python数字转对应中文的方法总结的详细内容,更多关于python数字怎么转对应中文的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
Python中多线程及程序锁浅析
Jan 21 Python
Python编码爬坑指南(必看)
Jun 10 Python
WINDOWS 同时安装 python2 python3 后 pip 错误的解决方法
Mar 16 Python
python实现下载pop3邮件保存到本地
Jun 19 Python
解决Python pandas plot输出图形中显示中文乱码问题
Dec 12 Python
python Pexpect 实现输密码 scp 拷贝的方法
Jan 03 Python
python面向对象实现名片管理系统文件版
Apr 26 Python
用django设置session过期时间的方法解析
Aug 05 Python
Python 用三行代码提取PDF表格数据
Oct 13 Python
关于Numpy中的行向量和列向量详解
Nov 30 Python
TensorFlow加载模型时出错的解决方式
Feb 06 Python
class类在python中获取金融数据的实例方法
Dec 10 Python
Python List remove()实例用法详解
Aug 02 #Python
Python中基础数据类型 set集合知识点总结
Aug 02 #Python
python unittest单元测试的步骤分析
Aug 02 #Python
python元组打包和解包过程详解
Aug 02 #Python
python字典进行运算原理及实例分享
Aug 02 #Python
Python中可变和不可变对象的深入讲解
Python基础数据类型tuple元组的概念与用法
Aug 02 #Python
You might like
php中突破基于HTTP_REFERER的防盗链措施(stream_context_create)
2011/03/29 PHP
Yii查询生成器(Query Builder)用法实例教程
2014/09/04 PHP
php堆排序实现原理与应用方法
2015/01/03 PHP
PHP入门教程之数学运算技巧总结
2016/09/11 PHP
注意!PHP 7中不要做的10件事
2016/09/18 PHP
PHP依赖注入(DI)和控制反转(IoC)详解
2017/06/12 PHP
js 三级关联菜单效果实例
2013/08/13 Javascript
jquery事件重复绑定的快速解决方法
2014/01/03 Javascript
JavaScript中的getTimezoneOffset()方法使用详解
2015/06/10 Javascript
js实现固定宽高滑动轮播图效果
2017/01/13 Javascript
easyui-edatagrid.js实现回车键结束编辑功能的实例
2017/04/12 Javascript
js canvas实现写字动画效果
2018/11/30 Javascript
Vue根据条件添加click事件的方式
2019/11/09 Javascript
一个超级简单的python web程序
2014/09/11 Python
使用rpclib进行Python网络编程时的注释问题
2015/05/06 Python
Python实现配置文件备份的方法
2015/07/30 Python
Python中模块与包有相同名字的处理方法
2017/05/05 Python
pyqt5简介及安装方法介绍
2018/01/31 Python
我就是这样学习Python中的列表
2019/06/02 Python
Python OpenCV 使用滑动条来调整函数参数的方法
2019/07/08 Python
Python参数类型以及常见的坑详解
2019/07/08 Python
Python实现随机取一个矩阵数组的某几行
2019/11/26 Python
Python对称的二叉树多种思路实现方法
2020/02/28 Python
浅谈pandas.cut与pandas.qcut的使用方法及区别
2020/03/03 Python
关于jupyter打开之后不能直接跳转到浏览器的解决方式
2020/04/13 Python
Python存储读取HDF5文件代码解析
2020/11/25 Python
Notino瑞典:购买香水和美容产品
2019/07/26 全球购物
Herschel美国官网:背包、手提袋及配件
2020/03/10 全球购物
企业内控岗位的职责
2014/02/07 职场文书
财务人员的自我评价范文
2014/03/03 职场文书
石油工程专业毕业生求职信
2014/04/13 职场文书
员工安全生产承诺书
2014/05/22 职场文书
《珍珠鸟》教学反思
2016/02/16 职场文书
python中%格式表达式实例用法
2021/06/18 Python
MySQL空间数据存储及函数
2021/09/25 MySQL
使用Spring处理x-www-form-urlencoded方式
2021/11/02 Java/Android