环境: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)
运行示例: