python实现数字规整(转中文)

2024-03-01 09:20

本文主要是介绍python实现数字规整(转中文),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.思路根据正则匹配数字类型比如手机号、年月日等进行相对的数字规整

话不多说直接上代码,有新的类型可以按照当前方案进行新增


import redef match_year_digit(match):m = str(match.group())relation = {'1': '一', '2': '二', '3': '三', '4': '四', '5': '五', '6': '六', '7': '七', '8': '八', '9': '九', '0': '零','年': '年'}return ''.join([relation[i] for i in m])def time_thin_filter(sequence):time_thin_reg = re.compile(r'(?P<hour_part>\d{1,2})[:|:](?P<minute_part>\d{1,2})([:|:])?(?P<second_part>(\d{1,2}))?')result = time_thin_reg.search(sequence)unit_list = ['时', '分', '']reg_list = []try:hour_part = result.group('hour_part')reg_list.append(match_common_number(hour_part))reg_list.append(unit_list[0])minute_part = result.group('minute_part')reg_list.append(match_common_number(minute_part))reg_list.append(unit_list[1])second_part = result.group('second_part')reg_list.append(match_common_number(second_part))reg_list.append(unit_list[2])except IndexError as e:print('')finally:return ''.join(reg_list)return ''def date_thin_filter(match):sequence = str(match.group())time_thin_reg = re.compile(r'(?P<year_part>[1|2]\d{3})[-|\/](?P<month_part>(1[0-2]|[1-9]))(-|\/)?(?P<day_part>(3[0-1]|2[0-9]|1[0-9]|0?[1-9]))?')result = time_thin_reg.search(sequence)unit_list = ['年', '月', '日']reg_list = []try:year_part = result.group('year_part')if re.findall('[21][0-9]{3}', year_part):year_part = re.sub(u'[21][0-9]{3}', match_year_digit, year_part)else:year_part = match_common_number(year_part)reg_list.append(year_part)reg_list.append(unit_list[0])month_part = result.group('month_part')reg_list.append(match_common_number(month_part))reg_list.append(unit_list[1])day_part = result.group('day_part')reg_list.append(match_common_number(day_part))reg_list.append(unit_list[2])except IndexError as e:print('')finally:print(sequence,''.join(reg_list))return ''.join(reg_list)def percent_than(match):m = str(match.group())percent_filter = re.compile(r'(?P<percent_part>\d[\.\d]+)(?P<percent_mark>%)')result = percent_filter.search(m)try:percent_part = result.group('percent_part')if percent_part:x = match_common_number(percent_part)s = re.sub(percent_filter, '百分之' + x, m)return sexcept Exception as e:pass
def iphone_replace(match):m = str(match.group())relation = {'1': '幺', '2': '二', '3': '三', '4': '四', '5': '五', '6': '六', '7': '七', '8': '八', '9': '九', '0': '零'}return ''.join([relation[i] for i in m])
def first_number_filter(sequence="", count=4):# 数字是“年份”的情况,返回转化成中文的结果# (1)过滤手机号\工号(有新增版式的可以按照当前方式新增)iphone_rule=r"(?<!\d)1\d{10,15}|10086|12345|(?<!\d)0\d+"if re.findall(iphone_rule, sequence):sequence = re.sub(iphone_rule, iphone_replace, sequence, count=count)# (1)过滤年份,上个世纪的,本世纪的可以不用if re.findall('[21][0-9]{3}年', sequence):sequence = re.sub(u'[21][0-9]{3}年', match_year_digit, sequence, count=count)# (2)过滤百分比if re.findall('\d[\.\d]+%', sequence):print(re.findall('\d[\.\d]+%', sequence))sequence = re.sub(u'\d[\.\d]+%', percent_than, sequence, count=count)# (3)过滤时间# time_filter:能匹配'12:09:00~12:31:30' '12:09:00'time_filter = re.compile(r'(?P<start_time_part>\d{1,2}([:|:]\d{1,2}){1,2})(?P<time_mark>(~|-)?)(?P<end_time_part>(\d{1,2}([:|:]\d{1,2}){1,2})?)')result = time_filter.search(sequence)time_substitude_part = ''try:start_time_part = result.group('start_time_part')time_substitude_part = time_substitude_part + time_thin_filter(start_time_part)time_mark_part = result.group('time_mark')time_substitude_part += '到'end_time_part = result.group('end_time_part')time_substitude_part += time_thin_filter(end_time_part)except Exception as e:print('')if time_substitude_part:sequence = re.sub(time_filter, time_substitude_part, sequence)# (4)过滤日期if re.findall('[1|2]\d{3}[-|\/](?:1[0-2]|[1-9])[-|\/](?:3[0-1]|2[0-9]|1[0-9]|0?[1-9])', sequence):print('aaa',re.findall('[1|2]\d{3}[-|\/](?:1[0-2]|[1-9])[-|\/](?:3[0-1]|2[0-9]|1[0-9]|0?[1-9])', sequence))sequence = re.sub(u'[1|2]\d{3}[-|\/](?:1[0-2]|[1-9])[-|\/](?:3[0-1]|2[0-9]|1[0-9]|0?[1-9])', date_thin_filter, sequence, count=count)print("ssss",sequence)# (last)过滤其他数字print(sequence,"s")return re.sub(u'[\d\.]+', match_common_number, sequence)recursive_depth = 0
def match_common_number(match):global recursive_depthrelation = {'1': '一', '2': '二', '3': '三', '4': '四', '5': '五', '6': '六', '7': '七', '8': '八', '9': '九', '0': '零','年': '年'}if type(match) == type("") and "." in match:match, match1 = match.split(".")mh1 = "".join([relation[i] for i in match1])# recursive_depth = 0number = match if (type(match) is type('')) else match.group()if "." in number:number, number1 = number.split(".")mh1 = "".join([relation[i] for i in number1])# 数字非年份的情况,返回转化成中文的结果str_number = str(number)if len(str_number) > 4:str_number = str_number[-4:]bits = "零 一 二 三 四 五 六 七 八 九".split(" ")units = " 十 百 千".split(" ")large_unit = ' 万 亿 兆'.split(" ")  # 可扩展,以万为单位number_len = len(str_number)result = ""for i in range(number_len):result += bits[int(str_number[i])]if str_number[i] != "0":result += units[number_len - i - 1]# 去除连续的零while "零零" in result:result = result.replace("零零", "零")# 去除尾部的零if result[-1] == "零":result = result[:-1]# 调整10~20之间的数if result[:2] == "一十":result = result[1:]try:result= result + "点" + mh1except Exception as e:pass# 字符串连接上大单位result += large_unit[recursive_depth]# print(result)# 判断是否递归if len(str(number)) > 4:recursive_depth += 1return first_number_filter(str(number)[:-4], recursive_depth) + resultelse:recursive_depth=0return resultif __name__ == '__main__':# print(first_number_filter("1989-12-12嘿1998-12-12"))# print(first_number_filter("1989年2024年55.5%h55.0%"))# print(first_number_filter("55.0%he2.0"))# print(first_number_filter("2024年02月01日"))# print(first_number_filter("1999-12-30he2024年02月01日,1922-12-30"))print(first_number_filter("1999-12-30,1922-12-8"))print(first_number_filter("10086和15221331963he1012"))

这篇关于python实现数字规整(转中文)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/761959

相关文章

Python中模块graphviz使用入门

《Python中模块graphviz使用入门》graphviz是一个用于创建和操作图形的Python库,本文主要介绍了Python中模块graphviz使用入门,具有一定的参考价值,感兴趣的可以了解一... 目录1.安装2. 基本用法2.1 输出图像格式2.2 图像style设置2.3 属性2.4 子图和聚

Python使用Matplotlib绘制3D曲面图详解

《Python使用Matplotlib绘制3D曲面图详解》:本文主要介绍Python使用Matplotlib绘制3D曲面图,在Python中,使用Matplotlib库绘制3D曲面图可以通过mpl... 目录准备工作绘制简单的 3D 曲面图绘制 3D 曲面图添加线框和透明度控制图形视角Matplotlib

一文教你Python如何快速精准抓取网页数据

《一文教你Python如何快速精准抓取网页数据》这篇文章主要为大家详细介绍了如何利用Python实现快速精准抓取网页数据,文中的示例代码简洁易懂,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录1. 准备工作2. 基础爬虫实现3. 高级功能扩展3.1 抓取文章详情3.2 保存数据到文件4. 完整示例

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

基于Python打造一个智能单词管理神器

《基于Python打造一个智能单词管理神器》这篇文章主要为大家详细介绍了如何使用Python打造一个智能单词管理神器,从查询到导出的一站式解决,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 项目概述:为什么需要这个工具2. 环境搭建与快速入门2.1 环境要求2.2 首次运行配置3. 核心功能使用指

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

利用Python打造一个Excel记账模板

《利用Python打造一个Excel记账模板》这篇文章主要为大家详细介绍了如何使用Python打造一个超实用的Excel记账模板,可以帮助大家高效管理财务,迈向财富自由之路,感兴趣的小伙伴快跟随小编一... 目录设置预算百分比超支标红预警记账模板功能介绍基础记账预算管理可视化分析摸鱼时间理财法碎片时间利用财

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B

Python中的Walrus运算符分析示例详解

《Python中的Walrus运算符分析示例详解》Python中的Walrus运算符(:=)是Python3.8引入的一个新特性,允许在表达式中同时赋值和返回值,它的核心作用是减少重复计算,提升代码简... 目录1. 在循环中避免重复计算2. 在条件判断中同时赋值变量3. 在列表推导式或字典推导式中简化逻辑