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中os.path用法分析
Jan 15 Python
Python挑选文件夹里宽大于300图片的方法
Mar 05 Python
Python使用cookielib模块操作cookie的实例教程
Jul 12 Python
Python实现一个服务器监听多个客户端请求
Apr 12 Python
python单例模式获取IP代理的方法详解
Sep 13 Python
python将txt文件读取为字典的示例
Dec 22 Python
python批量从es取数据的方法(文档数超过10000)
Dec 27 Python
Python3中的最大整数和最大浮点数实例
Jul 09 Python
解决Django加载静态资源失败的问题
Jul 28 Python
基于Python把网站域名解析成ip地址
May 25 Python
在Python3.74+PyCharm2020.1 x64中安装使用Kivy的详细教程
Aug 07 Python
python中数组和列表的简单实例
Mar 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
第二节--PHP5 的对象模型
2006/11/16 PHP
php中base64_decode与base64_encode加密解密函数实例
2014/11/24 PHP
js实现ASP分页函数 HTML分页函数
2006/09/22 Javascript
JQuery 图片延迟加载并等比缩放插件
2009/11/09 Javascript
JavaScript DOM 学习第二章 编辑文本
2010/02/19 Javascript
js判断为空Null与字符串为空简写方法
2014/02/24 Javascript
浅谈Angular中ngModel的$render
2016/10/24 Javascript
EsLint入门学习教程
2017/02/17 Javascript
vue2笔记 — vue-router路由懒加载的实现
2017/03/03 Javascript
vue开发调试神器vue-devtools使用详解
2017/07/13 Javascript
浅谈angularJS的$watch失效问题的解决方案
2017/08/11 Javascript
Vue实现购物车场景下的应用
2017/11/27 Javascript
nodejs实现一个word文档解析器思路详解
2018/08/14 NodeJs
angularJs中$scope数据序列化的实例
2018/09/30 Javascript
vue中使用protobuf的过程记录
2018/10/26 Javascript
vue-cli3 项目从搭建优化到docker部署的方法
2019/01/28 Javascript
python在多玩图片上下载妹子图的实现代码
2013/08/13 Python
python实现登陆知乎获得个人收藏并保存为word文件
2015/03/16 Python
Python使用CMD模块更优雅的运行脚本
2015/05/11 Python
Python3.2模拟实现webqq登录
2016/02/15 Python
python实现八大排序算法(2)
2017/09/14 Python
python 请求服务器的实现代码(http请求和https请求)
2018/05/25 Python
python删除文本中行数标签的方法
2018/05/31 Python
使用python判断jpeg图片的完整性实例
2019/06/10 Python
python中的句柄操作的方法示例
2019/06/20 Python
Python3使用腾讯云文字识别(腾讯OCR)提取图片中的文字内容实例详解
2020/02/18 Python
Python如何将函数值赋给变量
2020/04/28 Python
Hawes & Curtis澳大利亚官网:英国经典服饰品牌
2018/10/29 全球购物
建筑工程技术应届生求职信
2013/11/17 职场文书
酒店总经理职务说明书
2014/02/26 职场文书
乡镇党的群众路线对照检查材料
2014/09/24 职场文书
副乡长民主生活会个人对照检查材料思想汇报
2014/10/01 职场文书
2014年流动人口工作总结
2014/11/26 职场文书
巾帼文明岗事迹材料
2014/12/24 职场文书
出国留学英文自荐信
2015/03/25 职场文书
2019求职信:应届生求职信范文
2019/04/24 职场文书