本文主要是介绍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实现数字规整(转中文)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!