本文主要是介绍python大写中文转阿拉伯数字,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#!/usr/bin/env python
-- coding:utf-8 --
‘’’
@author: autobaba
@software: Pycharm
@file: word2number.py
@time: 20
@desc:
‘’’
支持万亿以内的转换
import re
def word2number(amount):
chinese_num = {‘零’: 0, ‘壹’: 1, ‘贰’: 2, ‘叁’: 3, ‘肆’: 4, ‘伍’: 5, ‘陆’: 6, ‘柒’: 7, ‘捌’: 8, ‘玖’: 9}
chinese_amount = {‘分’: 0.01, ‘角’: 0.1, ‘元’: 1, ‘拾’: 10, ‘佰’: 100, ‘仟’: 1000, ‘圆’: 1}
amount_float = 0
if ‘亿’ in amount:
yi = re.match(r’(.+)亿.‘, amount).group(1)
amount_yi = 0
for i in chinese_amount:
if i in yi:
amount_yi += chinese_num[yi[yi.index(i) - 1]] * chinese_amount[i]
if yi[-1] in chinese_num.keys():
amount_yi += chinese_num[yi[-1]]
amount_float += amount_yi * 100000000
amount = re.sub(r’.+亿’, ‘’, amount, count=1)
if ‘万’ in amount:
wan = re.match(r’(.+)万.‘, amount).group(1)
amount_wan = 0
for i in chinese_amount:
if i in wan:
amount_wan += chinese_num[wan[wan.index(i) - 1]] * chinese_amount[i]
if wan[-1] in chinese_num.keys():
amount_wan += chinese_num[wan[-1]]
amount_float += amount_wan * 10000
amount = re.sub(r’.+万’, ‘’, amount, count=1)
amount_yuan = 0
for i in chinese_amount:if i in amount:if amount[amount.index(i) - 1] in chinese_num.keys():amount_yuan += chinese_num[amount[amount.index(i) - 1]] * chinese_amount[i]
amount_float += amount_yuanreturn amount_float
print(word2number(“伍拾万伍仟柒佰玖拾叁元整”))
print(word2number(“壹佰柒拾万玖仟叁佰零伍元玖角伍分”))
print(word2number(“壹佰零柒元整”))
print(word2number(“壹仟零柒元整”))
chinese_num = {‘零’: 0, ‘壹’: 1, ‘贰’: 2, ‘叁’: 3, ‘肆’: 4, ‘伍’: 5, ‘陆’: 6, ‘柒’: 7, ‘捌’: 8, ‘玖’: 9}
chinese_amount = {‘分’: 0.01, ‘角’: 0.1, ‘元’: 1, ‘拾’: 10, ‘佰’: 100, ‘仟’: 1000, ‘圆’: 1}
这篇关于python大写中文转阿拉伯数字的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!