当前位置: 首页 > article >正文

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_yuan

return 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}


http://www.kler.cn/a/146949.html

相关文章:

  • Spring Events在大型项目中的最佳实践
  • [241115] Debian 12.8 发布 | Mistral AI 推出批量 API,成本降低 50%
  • 【Mysql】Mysql的多表查询---多表联合查询(上)
  • LlamaIndex+本地部署InternLM实践
  • 智能化运维与AI/ML辅助决策:实现自动化与预测优化
  • 学习threejs,使用第一视角控制器FirstPersonControls控制相机
  • C 中的指针 - 函数
  • Java游戏 王者荣耀
  • 【玩转client-go】使用client-go从POD拷贝文件出来
  • Android 13.0 开机过滤部分通知声音(莫名其妙的通知声音)
  • 蓝桥杯官网算法赛(蓝桥小课堂)
  • 做直播服务器要什么样的配置呢?
  • C语言做一个恶作剧关机程序
  • 大数据Doris(三十):删除数据(Delete)
  • RK356x U-Boot研究所(命令篇)3.11 mmc命令的用法
  • 论文阅读:“Model-based teeth reconstruction”
  • Python BDD 框架比较之 pytest-bdd vs behave
  • 14. 最长公共前缀
  • vue 中 asstes 和 static 有什么联系与区别
  • python-opencv 人脸68点特征点检测
  • git日历坐标系? 手动实现github活跃/贡献图
  • Android UnsatisfiedLinkError问题定位
  • AIGC系列之:DDPM原理解读(简单易懂版)
  • C语言——打印出所有的“水仙花数”
  • Day49:647. 回文子串、516.最长回文子序列
  • WPF实战项目十七(客户端):数据等待加载弹框动画