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

相关文章

Vue ElementUI中Upload组件批量上传的实现代码

《VueElementUI中Upload组件批量上传的实现代码》ElementUI中Upload组件批量上传通过获取upload组件的DOM、文件、上传地址和数据,封装uploadFiles方法,使... ElementUI中Upload组件如何批量上传首先就是upload组件 <el-upl

Docker部署Jenkins持续集成(CI)工具的实现

《Docker部署Jenkins持续集成(CI)工具的实现》Jenkins是一个流行的开源自动化工具,广泛应用于持续集成(CI)和持续交付(CD)的环境中,本文介绍了使用Docker部署Jenkins... 目录前言一、准备工作二、设置变量和目录结构三、配置 docker 权限和网络四、启动 Jenkins

Python3脚本实现Excel与TXT的智能转换

《Python3脚本实现Excel与TXT的智能转换》在数据处理的日常工作中,我们经常需要将Excel中的结构化数据转换为其他格式,本文将使用Python3实现Excel与TXT的智能转换,需要的可以... 目录场景应用:为什么需要这种转换技术解析:代码实现详解核心代码展示改进点说明实战演练:从Excel到

Python中常用的四种取整方式分享

《Python中常用的四种取整方式分享》在数据处理和数值计算中,取整操作是非常常见的需求,Python提供了多种取整方式,本文为大家整理了四种常用的方法,希望对大家有所帮助... 目录引言向零取整(Truncate)向下取整(Floor)向上取整(Ceil)四舍五入(Round)四种取整方式的对比综合示例应

python 3.8 的anaconda下载方法

《python3.8的anaconda下载方法》本文详细介绍了如何下载和安装带有Python3.8的Anaconda发行版,包括Anaconda简介、下载步骤、安装指南以及验证安装结果,此外,还介... 目录python3.8 版本的 Anaconda 下载与安装指南一、Anaconda 简介二、下载 An

如何使用CSS3实现波浪式图片墙

《如何使用CSS3实现波浪式图片墙》:本文主要介绍了如何使用CSS3的transform属性和动画技巧实现波浪式图片墙,通过设置图片的垂直偏移量,并使用动画使其周期性地改变位置,可以创建出动态且具有波浪效果的图片墙,同时,还强调了响应式设计的重要性,以确保图片墙在不同设备上都能良好显示,详细内容请阅读本文,希望能对你有所帮助...

Python自动化处理手机验证码

《Python自动化处理手机验证码》手机验证码是一种常见的身份验证手段,广泛应用于用户注册、登录、交易确认等场景,下面我们来看看如何使用Python自动化处理手机验证码吧... 目录一、获取手机验证码1.1 通过短信接收验证码1.2 使用第三方短信接收服务1.3 使用ADB读取手机短信1.4 通过API获取

C# string转unicode字符的实现

《C#string转unicode字符的实现》本文主要介绍了C#string转unicode字符的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随... 目录1. 获取字符串中每个字符的 Unicode 值示例代码:输出:2. 将 Unicode 值格式化

python安装whl包并解决依赖关系的实现

《python安装whl包并解决依赖关系的实现》本文主要介绍了python安装whl包并解决依赖关系的实现,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录一、什么是whl文件?二、我们为什么需要使用whl文件来安装python库?三、我们应该去哪儿下

Python脚本实现图片文件批量命名

《Python脚本实现图片文件批量命名》这篇文章主要为大家详细介绍了一个用python第三方库pillow写的批量处理图片命名的脚本,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录前言源码批量处理图片尺寸脚本源码GUI界面源码打包成.exe可执行文件前言本文介绍一个用python第三方库pi