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 相关文章推荐
Tornado服务器中绑定域名、虚拟主机的方法
Aug 22 Python
python实现两个文件合并功能
Apr 01 Python
分享一下Python数据分析常用的8款工具
Apr 29 Python
转换科学计数法的数值字符串为decimal类型的方法
Jul 16 Python
Python多线程原理与用法详解
Aug 20 Python
浅析python继承与多重继承
Sep 13 Python
Python基础教程之if判断,while循环,循环嵌套
Apr 25 Python
python Tkinter的图片刷新实例
Jun 14 Python
Python流程控制 if else实现解析
Sep 02 Python
sklearn-SVC实现与类参数详解
Dec 10 Python
关于PyCharm安装后修改路径名称使其可重新打开的问题
Oct 20 Python
python基础详解之if循环语句
Apr 24 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
discuz7 phpMysql操作类
2009/06/21 PHP
php 中文字符入库或显示乱码问题的解决方法
2010/04/12 PHP
解析将多维数组转换为支持curl提交的一维数组格式
2013/07/08 PHP
基于jquery的表头固定的若干方法
2011/01/27 Javascript
JS获取html对象的几种方式介绍
2013/12/05 Javascript
js实现仿爱微网两级导航菜单效果代码
2015/08/31 Javascript
JS中对Cookie的操作详解
2016/08/05 Javascript
JS无缝滚动效果实现方法分析
2016/12/21 Javascript
JS完成画圆圈的小球
2017/03/07 Javascript
vue-axios使用详解
2017/05/10 Javascript
bootstrap table单元格新增行并编辑
2017/05/19 Javascript
详解vue过滤器在v2.0版本用法
2017/06/01 Javascript
Vue学习笔记进阶篇之过渡状态详解
2017/07/14 Javascript
vue组件实现可搜索下拉框扩展
2020/10/23 Javascript
vue webpack打包后图片路径错误的完美解决方法
2018/12/07 Javascript
vue 集成jTopo 处理方法
2019/08/07 Javascript
Vue路由守卫及页面登录权限控制的设置方法(两种)
2020/03/31 Javascript
浅谈js中的attributes和Attribute的用法与区别
2020/07/16 Javascript
JS使用setInterval计时器实现挑战10秒
2020/11/08 Javascript
python解析html开发库pyquery使用方法
2014/02/07 Python
Python实现树莓派WiFi断线自动重连的实例代码
2017/03/16 Python
Python进度条实时显示处理进度的示例代码
2018/01/30 Python
解决Python 使用h5py加载文件,看不到keys()的问题
2019/02/08 Python
Django中多种重定向方法使用详解
2019/07/17 Python
Python 自动登录淘宝并保存登录信息的方法
2019/09/04 Python
python脚本后台执行方式
2019/12/21 Python
python 错误处理 assert详解
2020/04/20 Python
俄罗斯金苹果网上化妆品和香水商店:Goldapple
2019/12/01 全球购物
家长会主持词开场白
2014/03/18 职场文书
2013年最新自荐信范文
2014/06/23 职场文书
大学生入党积极分子自我评价
2014/09/20 职场文书
夫妻房产协议书的格式
2014/10/11 职场文书
租车协议书范本2014
2014/11/17 职场文书
会议欢迎词
2015/01/23 职场文书
2016年学校“6﹒26国际禁毒日”宣传活动总结
2016/04/05 职场文书
2019年公司卫生管理制度样本
2019/08/21 职场文书