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比较两个列表大小的方法
Jul 11 Python
python Django模板的使用方法
Jan 14 Python
python:socket传输大文件示例
Jan 18 Python
浅析Python函数式编程
Oct 06 Python
PyGame贪吃蛇的实现代码示例
Nov 21 Python
对python 命令的-u参数详解
Dec 03 Python
树莓派动作捕捉抓拍存储图像脚本
Jun 22 Python
浅谈PyQt5中异步刷新UI和Python多线程总结
Dec 13 Python
pyinstaller打包成无控制台程序时运行出错(与popen冲突的解决方法)
Apr 15 Python
基于python图书馆管理系统设计实例详解
Aug 05 Python
解决Ubuntu18中的pycharm不能调用tensorflow-gpu的问题
Sep 17 Python
使用Pytorch搭建模型的步骤
Nov 16 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实用代码片段
2016/02/02 PHP
Laravel框架Eloquent ORM修改数据操作示例
2019/12/03 PHP
javascript判断iphone/android手机横竖屏模式的函数
2011/12/20 Javascript
用原生JS获取CLASS对象(很简单实用)
2014/10/15 Javascript
jquery选择器需要注意的问题
2014/11/26 Javascript
浅谈javascript中for in 和 for each in的区别
2015/04/23 Javascript
JavaScript中函数(Function)的apply与call理解
2015/07/08 Javascript
cocos2dx骨骼动画Armature源码剖析(三)
2015/09/08 Javascript
jQuery获得字体颜色16位码的方法
2016/02/20 Javascript
JS获取中文拼音首字母并通过拼音首字母快速查找页面内对应中文内容的方法【附demo源码】
2016/08/19 Javascript
JS自定义函数对web前端上传的文件进行类型大小判断
2016/10/19 Javascript
详解Vue.js搭建路由报错 router.map is not a function
2017/06/27 Javascript
JavaScript判断浏览器运行环境的详细方法
2019/06/30 Javascript
MockJs结合json-server模拟后台数据
2020/08/26 Javascript
layui checkbox默认选中,获取选中值,清空所有选中项的例子
2019/09/02 Javascript
解决layui数据表格排序图标被超出的表头挤出去的问题
2019/09/19 Javascript
JavaScript中的惰性载入函数及优势
2020/02/18 Javascript
python模拟登录并且保持cookie的方法详解
2017/04/04 Python
pandas数据框,统计某列数据对应的个数方法
2018/04/11 Python
python模块smtplib学习
2018/05/22 Python
python生成1行四列全2矩阵的方法
2018/08/04 Python
选择python进行数据分析的理由和优势
2019/06/25 Python
Python 文件数据读写的具体实现
2020/01/24 Python
Python selenium爬取微信公众号文章代码详解
2020/08/12 Python
matplotlib绘制鼠标的十字光标的实现(内置方式)
2021/01/06 Python
澳大利亚连衣裙和女装在线:Esther
2017/11/11 全球购物
英国家电购物网站:Sonic Direct
2019/03/26 全球购物
商场拾金不昧表扬信
2014/01/13 职场文书
消防应急演练方案
2014/02/12 职场文书
银行贷款收入证明
2014/10/17 职场文书
离婚财产分配协议书
2014/10/21 职场文书
2014年爱国卫生工作总结
2014/11/22 职场文书
教师党员承诺书2015
2015/01/21 职场文书
小学信息技术教学反思
2016/02/16 职场文书
个人道歉信大全
2019/04/11 职场文书
分享:关于学习的励志名言赏析
2019/08/16 职场文书