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使用Tkinter显示网络图片的方法
Apr 24 Python
Java Web开发过程中登陆模块的验证码的实现方式总结
May 25 Python
尝试用最短的Python代码来实现服务器和代理服务器
Jun 23 Python
用python写个自动SSH登录远程服务器的小工具(实例)
Jun 17 Python
详解 Python 读写XML文件的实例
Aug 02 Python
Python基于回溯法子集树模板实现图的遍历功能示例
Sep 05 Python
python+opencv+caffe+摄像头做目标检测的实例代码
Aug 03 Python
python 字符串只保留汉字的方法
Nov 16 Python
python实现扫描ip地址的小程序
Apr 16 Python
pytorch中如何使用DataLoader对数据集进行批处理的方法
Aug 06 Python
Python 函数绘图及函数图像微分与积分
Nov 20 Python
基于Python制作一副扑克牌过程详解
Oct 19 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
php设计模式 Chain Of Responsibility (职责链模式)
2011/06/26 PHP
PHP中图片等比缩放的实例
2013/03/24 PHP
CURL状态码列表(详细)
2013/06/27 PHP
phpmailer发送邮件之后,返回收件人是否阅读了邮件的方法
2014/07/19 PHP
php处理复杂xml数据示例
2016/07/11 PHP
php使用自定义函数实现汉字分割替换功能示例
2017/01/30 PHP
Laravel框架定时任务2种实现方式示例
2018/12/08 PHP
基于Laravel-admin 后台的自定义页面用法详解
2019/09/30 PHP
php 实现简单的登录功能示例【基于thinkPHP框架】
2019/12/02 PHP
javascript时区函数介绍
2012/09/14 Javascript
js调试系列 初识控制台
2014/06/18 Javascript
javascript数组去重的方法汇总
2015/04/14 Javascript
Javascript页面跳转常见实现方式汇总
2015/11/28 Javascript
详解Angularjs中的依赖注入
2016/03/11 Javascript
ichart.js绘制虚线、平均分虚线效果的实现代码
2016/05/05 Javascript
关于JSON与JSONP简单总结
2016/08/16 Javascript
js实现把图片的绝对路径转为base64字符串、blob对象再上传
2016/12/29 Javascript
JavaScript实现公历转农历功能示例
2017/02/13 Javascript
微信小程序开发之数据存储 参数传递 数据缓存
2017/04/13 Javascript
微信小程序教程系列之设置标题栏和导航栏(7)
2020/06/29 Javascript
JS中Attr的用法详解
2017/10/09 Javascript
vue控制多行文字展开收起的实现示例
2019/10/11 Javascript
js实现盒子移动动画效果
2020/08/09 Javascript
[01:28]国服启动器接入蒸汽平台操作流程视频
2021/03/11 DOTA
使用python实现ANN
2017/12/20 Python
Python实现的FTP通信客户端与服务器端功能示例
2018/03/28 Python
python+opencv3.4.0 实现HOG+SVM行人检测的示例代码
2021/01/28 Python
英国著名的化妆品折扣网站:Allbeauty.com
2016/07/21 全球购物
欧缇丽加拿大官方网站:Caudalie加拿大
2019/07/18 全球购物
对于没有初始化的变量的初始值可以作怎样的假定
2014/10/12 面试题
J2EE的优越性主要表现在哪些方面
2016/03/28 面试题
毕业生简单求职信
2013/11/19 职场文书
管理学专业个人求职信范文
2013/12/13 职场文书
简短清晨问候语
2015/11/10 职场文书
go设置多个GOPATH的方式
2021/05/05 Golang
如何获取numpy array前N个最大值
2021/05/14 Python