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中list常用操作实例详解
Jun 03 Python
Android模拟器无法启动,报错:Cannot set up guest memory ‘android_arm’ Invalid argument的解决方法
Jul 01 Python
Python使用装饰器模拟用户登陆验证功能示例
Aug 24 Python
对Python Pexpect 模块的使用说明详解
Feb 14 Python
numpy:np.newaxis 实现将行向量转换成列向量
Nov 30 Python
安装PyInstaller失败问题解决
Dec 14 Python
python可视化text()函数使用详解
Feb 11 Python
vscode配置anaconda3的方法步骤
Aug 08 Python
python中entry用法讲解
Dec 04 Python
python温度转换华氏温度实现代码
Dec 06 Python
BeautifulSoup获取指定class样式的div的实现
Dec 07 Python
python 字典和列表嵌套用法详解
Jun 29 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实现图片添加水印功能
2014/02/13 PHP
Yii2 rbac权限控制操作步骤实例教程
2016/04/29 PHP
浅谈php://filter的妙用
2019/03/05 PHP
yii2的restful api路由实例详解
2019/05/14 PHP
php 使用 __call实现重载功能示例
2019/11/18 PHP
用cssText批量修改样式
2009/08/29 Javascript
jQuery学习基础知识小结
2010/11/25 Javascript
jquery关于事件冒泡和事件委托的技巧及阻止与允许事件冒泡的三种实现方法
2015/11/27 Javascript
jQuery formValidator表单验证
2016/01/07 Javascript
Bootstrap布局方式详解
2016/05/27 Javascript
JS扩展类,克隆对象与混合类实例分析
2016/11/26 Javascript
JS正则表达式修饰符global(/g)用法分析
2016/12/27 Javascript
jquery单击文字或图片内容放大并居中显示
2017/06/23 jQuery
解决Vue2.x父组件与子组件之间的双向绑定问题
2018/03/06 Javascript
vue.js项目 el-input 组件 监听回车键实现搜索功能示例
2018/08/25 Javascript
react-navigation之动态修改title的内容
2018/09/26 Javascript
vue实现滑动切换效果(仅在手机模式下可用)
2020/06/29 Javascript
OpenLayer学习之自定义测量控件
2020/09/28 Javascript
python3使用PyMysql连接mysql数据库实例
2017/02/07 Python
python使用KNN算法手写体识别
2018/02/01 Python
Python使用一行代码获取上个月是几月
2018/08/30 Python
Python实现多级目录压缩与解压文件的方法
2018/09/01 Python
python 控制Asterisk AMI接口外呼电话的例子
2019/08/08 Python
Python程序暂停的正常处理方法
2019/11/07 Python
python使用正则表达式去除中文文本多余空格,保留英文之间空格方法详解
2020/02/11 Python
python GUI库图形界面开发之PyQt5简单绘图板实例与代码分析
2020/03/08 Python
一文带你掌握Pyecharts地理数据可视化的方法
2021/02/06 Python
HTML5 Canvas中绘制椭圆的4种方法
2015/04/24 HTML / CSS
Bootstrap File Input文件上传组件
2020/12/01 HTML / CSS
生物技术专业研究生自荐信
2013/09/22 职场文书
安全生产活动月方案
2014/03/09 职场文书
机械工程及其自动化专业求职信
2014/08/08 职场文书
大学生读书笔记大全
2015/07/01 职场文书
中秋节祝酒词
2015/08/12 职场文书
Nginx服务器添加Systemd自定义服务过程解析
2021/03/31 Servers
Ajax实现三级联动效果
2021/10/05 Javascript