python人民幣小寫(xiě)轉(zhuǎn)大寫(xiě)輔助工具
本文實(shí)例為大家分享了python人民幣大小寫(xiě)轉(zhuǎn)換的具體代碼,供大家參考,具體內(nèi)容如下
大家應(yīng)該都知道,銀行打印賬單有時(shí)候會(huì)跟上人民幣的阿拉伯?dāng)?shù)字以及人民幣漢字大寫(xiě)寫(xiě)法,轉(zhuǎn)換的過(guò)程中有一定的邏輯難度,較為麻煩,所以筆者心血來(lái)潮,花了點(diǎn)時(shí)間簡(jiǎn)單實(shí)現(xiàn)了一下這一轉(zhuǎn)換過(guò)程,以供初學(xué)者參考。
輸入樣例:
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"萬(wàn)".decode("utf8"), u"拾".decode("utf8"), u"佰".decode("utf8"), u"仟".decode(
"utf8"), u"億".decode("utf8"), u"拾".decode("utf8"), u"佰".decode("utf8"), u"仟".decode("utf8"), u"萬(wàn)".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"零萬(wàn)".decode("utf8"),u"萬(wàn)".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()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python Pygame實(shí)現(xiàn)兔子獵人守護(hù)城堡游戲
這篇文章主要介紹了用python來(lái)制作的一個(gè)守護(hù)類(lèi)小游戲兔子獵人守護(hù)城堡,文中的示例代碼介紹得很詳細(xì),感興趣的小伙伴快來(lái)跟隨小編一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12
Pandas多級(jí)索引的實(shí)現(xiàn)示例
Pandas 中的多級(jí)索引是一種強(qiáng)大的工具,用于處理具有多個(gè)維度或?qū)哟蔚臄?shù)據(jù),本文主要介紹了Pandas多級(jí)索引的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01
Python利用三層神經(jīng)網(wǎng)絡(luò)實(shí)現(xiàn)手寫(xiě)數(shù)字分類(lèi)詳解
這篇文章主要介紹了如何設(shè)計(jì)一個(gè)三層神經(jīng)網(wǎng)絡(luò)模型來(lái)實(shí)現(xiàn)手寫(xiě)數(shù)字分類(lèi)。本文給大家介紹的非常詳細(xì),感興趣的小伙伴快來(lái)跟小編一起學(xué)習(xí)一下2021-11-11
OpenCV+face++實(shí)現(xiàn)實(shí)時(shí)人臉識(shí)別解鎖功能
這篇文章主要為大家詳細(xì)介紹了OpenCV+face++實(shí)現(xiàn)實(shí)時(shí)人臉識(shí)別解鎖功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08
利用Python演示數(shù)型數(shù)據(jù)結(jié)構(gòu)的教程
這篇文章主要介紹了利用Python演示數(shù)型數(shù)據(jù)結(jié)構(gòu)的教程,核心代碼其實(shí)只有一行(XD),需要的朋友可以參考下2015-04-04
Python實(shí)現(xiàn)微信好友的數(shù)據(jù)分析
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)微信好友的數(shù)據(jù)分析,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12
Python3 SSH遠(yuǎn)程連接服務(wù)器的方法示例
這篇文章主要介紹了Python3 SSH遠(yuǎn)程連接服務(wù)器的方法示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
Python實(shí)戰(zhàn)之單詞打卡統(tǒng)計(jì)
這篇文章主要介紹了Python實(shí)戰(zhàn)之單詞打卡統(tǒng)計(jì),文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04
Python+OpenCV實(shí)現(xiàn)旋轉(zhuǎn)文本校正方式
今天小編就為大家分享一篇Python+OpenCV實(shí)現(xiàn)旋轉(zhuǎn)文本校正方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01

