python人民币小写转大写辅助工具

 更新时间:2018年06月20日 15:21:04   作者:雨者  
这篇文章主要为大家详细介绍了python人民币小写转大写辅助工具,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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() 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Python3最长回文子串算法示例

    Python3最长回文子串算法示例

    这篇文章主要介绍了Python3最长回文子串算法,结合实例形式分析了暴力算法及动态规划算法判断最长回文子串的相关操作技巧,需要的朋友可以参考下
    2019-03-03
  • keras 两种训练模型方式详解fit和fit_generator(节省内存)

    keras 两种训练模型方式详解fit和fit_generator(节省内存)

    这篇文章主要介绍了keras 两种训练模型方式详解fit和fit_generator(节省内存),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-07-07
  • Python Web程序部署到Ubuntu服务器上的方法

    Python Web程序部署到Ubuntu服务器上的方法

    在本文记录了我在Ubuntu中部署Flask Web站点的过程, 其中包括用户创建、代码获取、Python3环境的安装、虚拟环境设置、uWSGI启动程序设置,并将Nginx作为前端反向代理,需要的朋友参考下吧
    2018-02-02
  • python try...finally...的实现方法

    python try...finally...的实现方法

    这篇文章主要介绍了python try...finally...的实现方法,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下
    2020-11-11
  • python实现静态服务器

    python实现静态服务器

    这篇文章主要为大家详细介绍了python实现静态服务器,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-09-09
  • 解析Python中的eval()、exec()及其相关函数

    解析Python中的eval()、exec()及其相关函数

    本篇文章主要介绍了解析Python中的eval()、exec()及其相关函数,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-12-12
  • python实现图片批量压缩

    python实现图片批量压缩

    这篇文章主要介绍了python实现图片批量压缩的方法,帮助大家更好的理解和学习使用python,感兴趣的朋友可以了解下
    2021-04-04
  • Python openpyxl读取单元格字体颜色过程解析

    Python openpyxl读取单元格字体颜色过程解析

    这篇文章主要介绍了Python openpyxl读取单元格字体颜色过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-09-09
  • python实现简易名片管理系统

    python实现简易名片管理系统

    这篇文章主要为大家详细介绍了python实现简易名片管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-04-04
  • Python使用Turtle库绘制一棵西兰花

    Python使用Turtle库绘制一棵西兰花

    今天小编就为大家分享一篇Python使用Turtle库绘制一棵西兰花,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-11-11

最新评论