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) if curr_digit == 10 ** 8: result = result + tmp result = result * curr_digit hnd_mln = hnd_mln * 10 ** 8 + result result = 0 tmp = 0 elif curr_digit == 10 ** 4: result = result + tmp result = result * curr_digit tmp = 0 elif curr_digit >= 10: tmp = 1 if tmp == 0 else tmp result = result + curr_digit * tmp tmp = 0 elif curr_digit is not None: tmp = tmp * 10 + curr_digit else: return result result = result + tmp result = result + hnd_mln return result
|