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实现面向对像的ASP程序实例
Nov 10 Python
用Python程序抓取网页的HTML信息的一个小实例
May 02 Python
Python实现Youku视频批量下载功能
Mar 14 Python
Python实现随机生成手机号及正则验证手机号的方法
Apr 25 Python
python命令行工具Click快速掌握
Jul 04 Python
Django文件存储 自己定制存储系统解析
Aug 02 Python
解决Pycharm中恢复被exclude的项目问题(pycharm source root)
Feb 14 Python
浅谈spring boot 集成 log4j 解决与logback冲突的问题
Feb 20 Python
Tensorflow之梯度裁剪的实现示例
Mar 08 Python
python opencv实现图片缺陷检测(讲解直方图以及相关系数对比法)
Apr 07 Python
详解Python中的编码问题(encoding与decode、str与bytes)
Sep 30 Python
TensorFlow的环境配置与安装方法
Feb 20 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
解析argc argv在php中的应用
2013/06/24 PHP
yii框架配置默认controller和action示例
2014/04/30 PHP
PHP获取指定月份第一天和最后一天的方法
2015/07/18 PHP
动态加载图片路径 保持JavaScript控件的相对独立性
2010/09/03 Javascript
Node.js入门教程:在windows和Linux上安装配置Node.js图文教程
2014/08/14 Javascript
Jquery实现图片预加载与延时加载的方法
2014/12/22 Javascript
js数组依据下标删除元素
2015/04/14 Javascript
jQuery实现弹出窗口中切换登录与注册表单
2015/06/05 Javascript
javascript中sort排序实例详解
2016/07/24 Javascript
JS树形菜单组件Bootstrap TreeView使用方法详解
2016/12/21 Javascript
Angular使用动态加载组件方法实现Dialog的示例
2018/05/11 Javascript
如何在vue里面优雅的解决跨域(路由冲突问题)
2019/01/20 Javascript
React通过redux-persist持久化数据存储的方法示例
2019/02/14 Javascript
微信小程序框架的页面布局代码
2019/08/17 Javascript
layui 中select下拉change事件失效的解决方法
2019/09/20 Javascript
JS实现贪吃蛇游戏
2019/11/15 Javascript
nodejs环境使用Typeorm连接查询Oracle数据
2019/12/05 NodeJs
PYTHON正则表达式 re模块使用说明
2011/05/19 Python
Python实现信用卡系统(支持购物、转账、存取钱)
2016/06/24 Python
Python如何获取系统iops示例代码
2016/09/06 Python
python编写微信远程控制电脑的程序
2018/01/05 Python
python中return不返回值的问题解析
2020/07/22 Python
Python 日期与时间转换的方法
2020/08/01 Python
基于logstash实现日志文件同步elasticsearch
2020/08/06 Python
Pycharm2020.1安装中文语言插件的详细教程(不需要汉化)
2020/08/07 Python
html5.2 dialog简介详解
2018/02/27 HTML / CSS
函授大专自我鉴定
2013/11/01 职场文书
工商治理实习生的自我评价
2014/01/15 职场文书
会议活动邀请函
2014/01/27 职场文书
秋季运动会广播稿大全
2014/02/17 职场文书
打造高效课堂实施方案
2014/03/22 职场文书
食堂厨师岗位职责
2014/08/25 职场文书
2015年档案管理员工作总结
2015/05/13 职场文书
2015最新婚礼主持词
2015/06/30 职场文书
python 制作一个gui界面的翻译工具
2021/05/14 Python
python中Matplotlib绘制直线的实例代码
2021/07/04 Python