效果说明
输入《一百五十三》转换为153。参考了百度来的代码(http://www.jb51.net/article/114789.htm),稍微改了一下符合python3和一些exception的处理
代码
# 把汉字变为阿拉伯数字
def chinese2digits(chinese_str):
t = chinese_str
if t is None or t.strip() == "":
raise Exception("input error for %s" % chinese_str)
t = t.strip()
t = t.replace("百十", "百一十")
common_used_numerals = {'零': 0, '一': 1, '二': 2, '两': 2, '三': 3, '四': 4, '五': 5, '六': 6, '七': 7, '八': 8, '九': 9,
'十': 10, '百': 100, '千': 1000, '万': 10000, '亿': 100000000}
total = 0
r = 1
for i in range(len(t) - 1, -1, -1):
val = common_used_numerals.get(t[i])
if val is None:
raise Exception("%s can not be accepted." % t[i])
if val >= 10 and i == 0:
if val > r:
r = val
total = total + val
else:
r = r * val
elif val >= 10:
if val > r:
r = val
else:
r = r * val
else:
total = total + r * val
return total
使用方法:
a = chinese2digits("二百十")
print(a)