大体思路:
利用中文数字和计数单位交替出现的特点,设定好计数单位出现的顺序,从低往高交替搜索数字和计数单位,预期的数字或单位没有出现时都补0;
对中文数字第一个字是“十”的首先在之前加“一”;
def chinese2digit(chinese_num): units = "亿万千百十亿千百十万千百十" if chinese_num.startswith("十"): chinese_num = "一" + chinese_num k = len(units) - 1 d = "" i = len(chinese_num) - 1 find_num = True while i >= 0: if find_num: if nums.get(chinese_num[i]) is not None: # 找到一个数字 d = str(nums.get(chinese_num[i])) + d i = i - 1 else: # 没有找到数字,补0 d = '0' + d find_num = False else: if chinese_num[i] == units[k]: # 找到匹配的单位,准备找下一个数字 i = i - 1 k = k - 1 find_num = True else: if chinese_num[i] == '零': i = i - 1 # 没有找到匹配的单位:补零并匹配下一级单位 d = '0' + d k = k - 1 return d if __name__ == '__main__': print(chinese2digit('零')) print(chinese2digit('五')) print(chinese2digit('十')) print(chinese2digit('十五')) print(chinese2digit('一十五')) print(chinese2digit('一百零五')) print(chinese2digit('十三亿零五十三万六千七百一十三')) print(chinese2digit('三千万亿')) print(chinese2digit('三千万亿六千万零一十三'))
标签:__,中文数字,chinese,python,阿拉伯数字,num,print,chinese2digit From: https://www.cnblogs.com/learnthenknow/p/17190726.html