def dig2chars(s): """ 五百一十二 -->512 一三二 --> 132 """ # 使用unicode编码 fl = unicode(str(s).decode('gbk').encode('utf-8'), 'utf-8') # 先把数字提取 part = re.compile(r'[〇一二三四五六七八九十百]'.decode('utf-8')) fl = part.findall(fl) fl = ''.join(fl) # 没有百十千是另一种方法处理 part2 = re.compile(r'[〇一二三四五六七八九]'.decode('utf-8')) res2 = part2.findall(fl) result = 0 res_str = '' r = 1 fl_l = list(unicode(str(fl), 'utf-8')) china_dig = {u'〇': 0, u'一': 1, u'二': 2, u'三': 3, u'四': 4, u'五': 5, u'六': 6, u'七': 7, u'八': 8, u'九': 9, u'十': 10, u'百': 100} # 反向遍历 if u'十' in fl_l or u'百' in fl_l: for i in range(len(fl_l) - 1, -1, -1): val = china_dig.get(fl_l[i]) if val: # 处理十三 十四 if val >= 10 and i == 0: if val > r: r = val result = result + val else: r = r * val elif val >= 10: if val > r: r = val else: r = r * val else: result = result + r * val elif res2: for i in range(len(fl_l)): val = china_dig.get(fl_l[i]) if val: res_str += str(val) result = res_str else: result = s else: result = s return result