美文网首页
中文数字转阿拉伯数字

中文数字转阿拉伯数字

作者: 程猿先生 | 来源:发表于2018-06-29 20:11 被阅读0次

拿去用!!!

cn_dict = {
        u'一': '1', u'二': '2', u'三': '3', u'四': '4', u'五': '5', u'六': '6',
        u'七': '7', u'八': '8', u'九': '9', u'十': '10', u'百': '100', u'千': '1000',
    }


    def __cn2num(self, order_list):
        """将汉字转为数据"""
        order_list = [o.strip() for o in order_list]
        o_list = []
        for order in order_list:
            if order.isdigit():
                o_list.append(order)
            else:
                for i in order:
                    if i not in self.cn_dict:
                        return None
                l = len(order)  #order contain chinese number
                if l == 1:
                    o_list.append(self.cn_dict[order])
                elif l == 2:
                    if u'十' in order:
                        if order.index(u'十'):
                            o_list.append(self.cn_dict[order[0]] + '0')
                        else:
                            o_list.append('1' + self.cn_dict[order[1]])
                    elif u'百' in order:
                        o_list.append(self.cn_dict[order[0]] + '00')
                    else:
                        print "***"*20
                        print order
                elif l == 3:
                    if order.index(u'十') == 1:
                        o_list.append(self.cn_dict[order[0]] + self.cn_dict[order[2]])
                else:
                    return None
        return ','.join(o_list)

相关文章

网友评论

      本文标题:中文数字转阿拉伯数字

      本文链接:https://www.haomeiwen.com/subject/occzyftx.html