Python实现阿拉伯数字转汉语数字(范围:亿)

it2022-05-05  136

环境:win10+Python3  实现:亿范围内正整数阿拉伯数字转汉语数字(未做健壮性判断)

1 import re 2 3 4 num_dict = {'1':'', '2':'', '3':'', '4':'', '5':'', '6':'', '7':'', '8':'', '9':'', '0':'', } 5 index_dict = {1:'', 2:'', 3:'', 4:'', 5:'', 6:'', 7:'', 8:'', 9:'亿'} 6 while True: 7 num = input(">>") 8 9 nums = list(num) 10 nums_index = [x for x in range(1, len(nums)+1)][-1::-1] 11 12 str = '' 13 for index, item in enumerate(nums): 14 str = "".join((str, num_dict[item], index_dict[nums_index[index]])) 15 16 str = re.sub("零[十百千零]*", "", str) 17 str = re.sub("零万", "", str) 18 str = re.sub("亿万", "亿零", str) 19 str = re.sub("零零", "", str) 20 str = re.sub("零\\b" , "", str) 21 print(str)

运行示例:

转载于:https://www.cnblogs.com/LoveOpenSource/p/7554474.html

相关资源:各显卡算力对照表!

最新回复(0)