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

相关文章

SpringBoot集成redisson实现延时队列教程

《SpringBoot集成redisson实现延时队列教程》文章介绍了使用Redisson实现延迟队列的完整步骤,包括依赖导入、Redis配置、工具类封装、业务枚举定义、执行器实现、Bean创建、消费... 目录1、先给项目导入Redisson依赖2、配置redis3、创建 RedissonConfig 配

Python的Darts库实现时间序列预测

《Python的Darts库实现时间序列预测》Darts一个集统计、机器学习与深度学习模型于一体的Python时间序列预测库,本文主要介绍了Python的Darts库实现时间序列预测,感兴趣的可以了解... 目录目录一、什么是 Darts?二、安装与基本配置安装 Darts导入基础模块三、时间序列数据结构与

Python正则表达式匹配和替换的操作指南

《Python正则表达式匹配和替换的操作指南》正则表达式是处理文本的强大工具,Python通过re模块提供了完整的正则表达式功能,本文将通过代码示例详细介绍Python中的正则匹配和替换操作,需要的朋... 目录基础语法导入re模块基本元字符常用匹配方法1. re.match() - 从字符串开头匹配2.

Python使用FastAPI实现大文件分片上传与断点续传功能

《Python使用FastAPI实现大文件分片上传与断点续传功能》大文件直传常遇到超时、网络抖动失败、失败后只能重传的问题,分片上传+断点续传可以把大文件拆成若干小块逐个上传,并在中断后从已完成分片继... 目录一、接口设计二、服务端实现(FastAPI)2.1 运行环境2.2 目录结构建议2.3 serv

C#实现千万数据秒级导入的代码

《C#实现千万数据秒级导入的代码》在实际开发中excel导入很常见,现代社会中很容易遇到大数据处理业务,所以本文我就给大家分享一下千万数据秒级导入怎么实现,文中有详细的代码示例供大家参考,需要的朋友可... 目录前言一、数据存储二、处理逻辑优化前代码处理逻辑优化后的代码总结前言在实际开发中excel导入很

通过Docker容器部署Python环境的全流程

《通过Docker容器部署Python环境的全流程》在现代化开发流程中,Docker因其轻量化、环境隔离和跨平台一致性的特性,已成为部署Python应用的标准工具,本文将详细演示如何通过Docker容... 目录引言一、docker与python的协同优势二、核心步骤详解三、进阶配置技巧四、生产环境最佳实践

Python一次性将指定版本所有包上传PyPI镜像解决方案

《Python一次性将指定版本所有包上传PyPI镜像解决方案》本文主要介绍了一个安全、完整、可离线部署的解决方案,用于一次性准备指定Python版本的所有包,然后导出到内网环境,感兴趣的小伙伴可以跟随... 目录为什么需要这个方案完整解决方案1. 项目目录结构2. 创建智能下载脚本3. 创建包清单生成脚本4

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF

Nginx部署HTTP/3的实现步骤

《Nginx部署HTTP/3的实现步骤》本文介绍了在Nginx中部署HTTP/3的详细步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录前提条件第一步:安装必要的依赖库第二步:获取并构建 BoringSSL第三步:获取 Nginx

MyBatis Plus实现时间字段自动填充的完整方案

《MyBatisPlus实现时间字段自动填充的完整方案》在日常开发中,我们经常需要记录数据的创建时间和更新时间,传统的做法是在每次插入或更新操作时手动设置这些时间字段,这种方式不仅繁琐,还容易遗漏,... 目录前言解决目标技术栈实现步骤1. 实体类注解配置2. 创建元数据处理器3. 服务层代码优化填充机制详