添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

脚本很简单,唯一需要思考一下的是,为什么「万」和「亿」不能与「千」、「百」、「十」放在一起处理。这个问题想通了,代码也就好理解了。

不多说,上代码。

convertChineseDigitsToArabic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
chs_arabic_map = {u'零':0, u'一':1, u'二':2, u'三':3, u'四':4,
u'五':5, u'六':6, u'七':7, u'八':8, u'九':9,
u'十':10, u'百':100, u'千':10 ** 3, u'万':10 ** 4,
u'〇':0, u'壹':1, u'贰':2, u'叁':3, u'肆':4,
u'伍':5, u'陆':6, u'柒':7, u'捌':8, u'玖':9,
u'拾':10, u'佰':100, u'仟':10 ** 3, u'萬':10 ** 4,
u'亿':10 ** 8, u'億':10 ** 8, u'幺': 1,
u'0':0, u'1':1, u'2':2, u'3':3, u'4':4,
u'5':5, u'6':6, u'7':7, u'8':8, u'9':9}

def convertChineseDigitsToArabic (chinese_digits, encoding="utf-8"):
if isinstance (chinese_digits, str):
chinese_digits = chinese_digits.decode (encoding)

result = 0
tmp = 0
hnd_mln = 0
for count in range(len(chinese_digits)):
curr_char = chinese_digits[count]
curr_digit = chs_arabic_map.get(curr_char, None)
# meet 「亿」 or 「億」
if curr_digit == 10 ** 8:
result = result + tmp
result = result * curr_digit
# get result before 「亿」 and store it into hnd_mln
# reset `result`
hnd_mln = hnd_mln * 10 ** 8 + result
result = 0
tmp = 0
# meet 「万」 or 「萬」
elif curr_digit == 10 ** 4:
result = result + tmp
result = result * curr_digit
tmp = 0
# meet 「十」, 「百」, 「千」 or their traditional version
elif curr_digit >= 10:
tmp = 1 if tmp == 0 else tmp
result = result + curr_digit * tmp
tmp = 0
# meet single digit
elif curr_digit is not None:
tmp = tmp * 10 + curr_digit
else:
return result
result = result + tmp
result = result + hnd_mln
return result

完整版见: GitHub Repo

测试用例来自: fayaa