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通过get,post方式发送http请求和接收http响应的方法
May 26 Python
python根据京东商品url获取产品价格
Aug 09 Python
Python每天必学之bytes字节
Jan 28 Python
python出现&quot;IndentationError: unexpected indent&quot;错误解决办法
Oct 15 Python
python pandas 对series和dataframe的重置索引reindex方法
Jun 07 Python
Python把csv数据写入list和字典类型的变量脚本方法
Jun 15 Python
Python异常的检测和处理方法
Oct 26 Python
解决Python中定时任务线程无法自动退出的问题
Feb 18 Python
Python I/O与进程的详细讲解
Mar 08 Python
django 使用 PIL 压缩图片的例子
Aug 16 Python
Python pyautogui模块实现鼠标键盘自动化方法详解
Feb 17 Python
django执行数据库查询之后实现返回的结果集转json
Mar 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
php visitFile()遍历指定文件夹函数
2010/08/21 PHP
Symfony2中被遗弃的getRequest()方法分析
2016/03/17 PHP
jquery插件开发注意事项小结
2013/06/04 Javascript
使用javascript为网页增加夜间模式
2014/01/26 Javascript
JavaScript实现动态创建CSS样式规则方案
2014/09/06 Javascript
jQuery中:empty选择器用法实例
2014/12/30 Javascript
Javascript数组操作函数总结
2015/02/05 Javascript
js获取iframe中的window对象的实现方法
2016/05/20 Javascript
jQuery获取及设置表单input各种类型值的方法小结
2016/05/24 Javascript
JS实现求数组起始项到终止项之和的方法【基于数组扩展函数】
2017/06/13 Javascript
JavaScript实现跟随滚动缓冲运动广告框
2017/07/15 Javascript
mongoose设置unique不生效问题的解决及如何移除unique的限制
2017/11/07 Javascript
Angularjs之如何在跨域请求中传输Cookie的方法
2018/06/01 Javascript
js属性对象的hasOwnProperty方法的使用
2021/02/05 Javascript
python进程类subprocess的一些操作方法例子
2014/11/22 Python
Python自然语言处理之词干,词形与最大匹配算法代码详解
2017/11/16 Python
python实现读取大文件并逐行写入另外一个文件
2018/04/19 Python
pygame游戏之旅 按钮上添加文字的方法
2018/11/21 Python
python 利用pywifi模块实现连接网络破解wifi密码实时监控网络
2019/09/16 Python
浅谈python累加求和+奇偶数求和_break_continue
2020/02/25 Python
Python结合百度语音识别实现实时翻译软件的实现
2021/01/18 Python
Python页面加载的等待方式总结
2021/02/28 Python
CSS3+js实现简单的时钟特效
2015/03/18 HTML / CSS
新秀丽官方旗舰店:Samsonite拉杆箱、双肩包、皮具
2018/03/05 全球购物
现代家居用品及礼品:LBC Modern
2018/06/24 全球购物
2019年Java面试必问之经典试题
2012/09/12 面试题
什么是触发器(trigger)? 触发器有什么作用?
2013/09/18 面试题
什么是虚拟内存?虚拟内存有什么优势?
2016/02/09 面试题
大学生学习党课思想汇报
2014/01/03 职场文书
出生证明公证书
2014/04/09 职场文书
餐饮投资计划书
2014/04/25 职场文书
司法局群众路线教育实践活动开展情况总结
2014/10/25 职场文书
2014年信贷员工作总结
2014/11/18 职场文书
python中的sys模块和os模块
2022/03/20 Python
浅谈Redis 中的过期删除策略和内存淘汰机制
2022/04/03 Redis