python人民币小写转大写辅助工具


Posted in Python onJune 20, 2018

本文实例为大家分享了python人民币大小写转换的具体代码,供大家参考,具体内容如下

大家应该都知道,银行打印账单有时候会跟上人民币的阿拉伯数字以及人民币汉字大写写法,转换的过程中有一定的逻辑难度,较为麻烦,所以笔者心血来潮,花了点时间简单实现了一下这一转换过程,以供初学者参考。

输入样例:

123.22

输出样例:

壹佰贰拾叁圆贰角贰分

参考代码:

#!/usr/bin/env python 
# encoding: utf-8 
 
from __future__ import print_function 
import sys 
import re 
import base64 
import time 
import os 
import getpass 
 
reload(sys) 
 
sys.setdefaultencoding("utf-8") 
 
 
char_arr_01 = [u"零".decode("utf8"), u"壹".decode("utf8"), u"贰".decode("utf8"), u"叁".decode("utf8"), u"肆".decode( 
  "utf8"), u"伍".decode("utf8"), u"陆".decode("utf8"), u"柒".decode("utf8"), u"捌".decode("utf8"), u"玖".decode("utf8")]; 
char_arr_02 = [u"圆".decode("utf8"), u"拾".decode("utf8"), u"佰".decode("utf8"), u"仟".decode("utf8"), u"万".decode("utf8"), u"拾".decode("utf8"), u"佰".decode("utf8"), u"仟".decode( 
  "utf8"), u"亿".decode("utf8"), u"拾".decode("utf8"), u"佰".decode("utf8"), u"仟".decode("utf8"), u"万".decode("utf8"), u"拾".decode("utf8"), u"佰".decode("utf8")] 
char_arr_03 = [u"分".decode("utf8"), u"角".decode("utf8")] 
 
def calcRMB(): 
  sum_arr = [] 
  in_str_dot = "" 
  in_str_Big = "" 
  flag = 0 
  dot_count = 0 
  in_str = raw_input("Please input number : ") 
  for i in in_str: 
    if i == '.': 
      dot_count += 1 
    elif ord(i) <= ord('z') and ord(i) >= ord('A'): 
      print("Error") 
      return 
  if len(in_str) > 12 or dot_count > 1: 
    print("Error") 
    return 
  in_str = unicode(in_str).decode("utf8") 
  out_str = "" 
  if in_str.find('.') != -1: 
    flag = 1 
  sum_arr = in_str.split('.') 
  in_str_Big = sum_arr[0] 
  if flag==1: 
    in_str_dot = sum_arr[1] 
  for i in range(len(in_str_Big)): 
    if cmp(in_str_Big[i],'0') == 0 and (len(in_str_Big)-1-i)%4 != 0: 
      out_str = out_str + char_arr_01[ord(in_str_Big[i])-ord('0')] 
    else: 
      out_str = out_str + char_arr_01[ord(in_str_Big[i])-ord('0')] 
      out_str = out_str + char_arr_02[len(in_str_Big)-1-i] 
  while out_str.find(u"零零".decode("utf8")) != -1: 
    out_str = out_str.replace(u"零零".decode("utf8"), u"零".decode("utf8")) 
  out_str = out_str.replace(u"零亿".decode("utf8"),u"亿".decode("utf8")) 
  out_str = out_str.replace(u"零万".decode("utf8"),u"万".decode("utf8")) 
  if out_str != u"零元".decode("utf8"): 
    out_str = out_str.replace(u"零圆".decode("utf8"),u"圆".decode("utf8")) 
  if len(in_str_dot) > 2 and flag == 1: 
    print("False !!") 
    return 
  if flag == 1: 
    for i in range(len(in_str_dot)): 
      out_str = out_str + char_arr_01[ord(in_str_dot[i])-ord('0')] 
      out_str = out_str + char_arr_03[len(in_str_dot)-1-i] 
 
  print(out_str) 
 
def main(): 
  while 1: 
    os.system("cls") 
    calcRMB() 
    print() 
    end_flag = raw_input("Try Again ? (y/n)") 
    if end_flag == 'y' or end_flag == 'Y': 
      continue 
    elif end_flag == 'n' or end_flag == 'N': 
      break 
    else: 
      print("\nError!!") 
      break 
 
if __name__ == '__main__': 
  main()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
布同自制Python函数帮助查询小工具
Mar 13 Python
python time模块用法实例详解
Sep 11 Python
举例讲解Python中的算数运算符的用法
May 13 Python
在Python的Django框架中生成CSV文件的方法
Jul 22 Python
实例详解Python模块decimal
Jun 26 Python
python manage.py runserver流程解析
Nov 08 Python
Python高级特性——详解多维数组切片(Slice)
Nov 26 Python
解决Pytorch 加载训练好的模型 遇到的error问题
Jan 10 Python
python torch.utils.data.DataLoader使用方法
Apr 02 Python
Python matplotlib画图时图例说明(legend)放到图像外侧详解
May 16 Python
pycharm中使用request和Pytest进行接口测试的方法
Jul 31 Python
Flask处理Web表单的实现方法
Jan 31 Python
python简易远程控制单线程版
Jun 20 #Python
python通过Windows下远程控制Linux系统
Jun 20 #Python
Python实现求解一元二次方程的方法示例
Jun 20 #Python
python实现NB-IoT模块远程控制
Jun 20 #Python
Python中pandas模块DataFrame创建方法示例
Jun 20 #Python
python自动发送邮件脚本
Jun 20 #Python
Python使用numpy模块创建数组操作示例
Jun 20 #Python
You might like
跟我学小偷程序之成功偷取首页(第三天)
2006/10/09 PHP
操作Oracle的php类
2006/10/09 PHP
介绍php设计模式中的工厂模式
2008/06/12 PHP
thinkPHP多域名情况下使用memcache方式共享session数据的实现方法
2016/07/21 PHP
详解PHP发送邮件知识点
2018/05/06 PHP
PHP程序员学习使用Swoole的理由
2018/06/24 PHP
Mootools 1.2教程 定时器和哈希简介
2009/09/15 Javascript
js获取单元格自定义属性值的代码(IE/Firefox)
2010/04/05 Javascript
在js中判断checkboxlist(.net控件客户端id)是否有选中
2013/04/11 Javascript
NodeJS与Mysql的交互示例代码
2013/08/18 NodeJs
jquery使用$(element).is()来判断获取的tagName
2014/08/24 Javascript
JS跨域问题详解
2014/11/25 Javascript
nodejs事件的监听与触发的理解分析
2015/02/12 NodeJs
JavaScript通过代码调用Flash显示的方法
2016/02/02 Javascript
教你JS中的运算符乘方、开方及变量格式转换
2016/08/09 Javascript
多种方式实现js图片预览
2016/12/12 Javascript
vue教程之toast弹框全局调用示例详解
2020/08/24 Javascript
使用javaScript实现鼠标拖拽事件
2020/04/03 Javascript
vue 巧用过渡效果(小结)
2018/09/22 Javascript
vue中使用props传值的方法
2019/05/08 Javascript
安装多版本Vue-CLI的实现方法
2020/03/24 Javascript
JS如何监听div的resize事件详解
2020/12/03 Javascript
使用python和Django完成博客数据库的迁移方法
2018/01/05 Python
简单实现python画圆功能
2018/01/25 Python
使用django-crontab实现定时任务的示例
2018/02/26 Python
对python修改xml文件的节点值方法详解
2018/12/24 Python
Python高级特性——详解多维数组切片(Slice)
2019/11/26 Python
猫咪家具:CatsPlay
2018/11/03 全球购物
自动化专业个人求职信范文
2013/12/30 职场文书
法定代表人授权委托书范文
2014/08/02 职场文书
四风专项整治工作情况汇报
2014/10/28 职场文书
2014超市收银员工作总结
2014/11/13 职场文书
2014年综治维稳工作总结
2014/11/17 职场文书
新郎新娘答谢词
2015/01/04 职场文书
客服专员岗位职责
2015/02/10 职场文书
详解Spring Boot使用系统参数表提升系统的灵活性
2021/06/30 Java/Android