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深入学习之对象的属性
Aug 31 Python
使用Python的Tornado框架实现一个简单的WebQQ机器人
Apr 24 Python
Python实现信用卡系统(支持购物、转账、存取钱)
Jun 24 Python
基于python中pygame模块的Linux下安装过程(详解)
Nov 09 Python
Django中的Model操作表的实现
Jul 24 Python
Flask模拟实现CSRF攻击的方法
Jul 24 Python
python交换两个变量的值方法
Jan 12 Python
用Pycharm实现鼠标滚轮控制字体大小的方法
Jan 15 Python
python 实现创建文件夹和创建日志文件的方法
Jul 07 Python
python 代码运行时间获取方式详解
Sep 18 Python
python3 os进行嵌套操作的实例讲解
Nov 19 Python
Python 将代码转换为可执行文件脱离python环境运行(步骤详解)
Jan 25 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
深入掌握include_once与require_once的区别
2013/06/17 PHP
javascript之卸载鼠标事件的代码
2007/05/14 Javascript
百度留言本js 大家可以参考下
2009/10/13 Javascript
jQuery+CSS3实现树叶飘落特效
2015/02/01 Javascript
微信小程序 loading(加载中提示框)实例
2016/10/28 Javascript
Javascript中八种遍历方法的执行速度深度对比
2017/04/25 Javascript
JavaScript编写的网页小游戏,很给力
2017/08/18 Javascript
浅谈Node.js之异步流控制
2017/10/25 Javascript
anime.js 实现带有描边动画效果的复选框(推荐)
2017/12/24 Javascript
详解Angular操作cookies方法
2018/06/01 Javascript
vue 的点击事件获取当前点击的元素方法
2018/09/15 Javascript
jQuery 同时获取多个标签的指定内容并储存为数组
2018/11/20 jQuery
js实现json数组分组合并操作示例
2019/02/12 Javascript
使用Vue.js 和Chart.js制作绚丽多彩的图表
2019/06/15 Javascript
Django 中使用流响应处理视频的方法
2018/07/20 Python
在pycharm中python切换解释器失败的解决方法
2018/10/29 Python
详解python tkinter教程-事件绑定
2019/03/28 Python
Python使用matplotlib绘制Logistic曲线操作示例
2019/11/28 Python
tensorflow求导和梯度计算实例
2020/01/23 Python
Python的pygame安装教程详解
2020/02/10 Python
HTML5 Canvas像素处理使用接口介绍
2012/12/02 HTML / CSS
HTML5新增属性data-*和js/jquery之间的交互及注意事项
2017/08/08 HTML / CSS
亚马逊加拿大网站:Amazon.ca
2020/01/06 全球购物
思想政治自我鉴定
2013/10/06 职场文书
机械设计职业生涯规划书
2013/12/27 职场文书
销售提升方案
2014/06/07 职场文书
小学班级口号
2014/06/09 职场文书
伊琍体标语
2014/06/25 职场文书
教师师德考核自我评价
2014/09/13 职场文书
2014年信贷员工作总结
2014/11/18 职场文书
个人创业事迹材料
2014/12/30 职场文书
受资助学生感谢信
2015/01/21 职场文书
详解CSS伪元素的妙用单标签之美
2021/05/25 HTML / CSS
python状态机transitions库详解
2021/06/02 Python
SSM项目使用拦截器实现登录验证功能
2022/01/22 Java/Android
Android开发手册TextInputLayout样式使用示例
2022/06/10 Java/Android