Python
Python2
漢数字
万進法

漢数字からintへの変換は上の桁からやったほうが

個人的に必要に迫られたので作ってみた。今更python2ですスイマセン。

https://qiita.com/kamawanu/items/f5bc6967ae57a1f50194

jpnumeric.py
#!/usr/bin/env python2
# !-*- coding:utf-8 -*-

import re

qnums = [
    u"00零", u"11一壱", u"22二弐", u"33三参", u"44四", u"55五伍", u"66六", u"77七", u"88八", u"99九",
    u"万萬", u"十拾", u"千阡",
]

maybenums1 = "".join(qnums)

qnums = [list(x) for x in qnums]
qnummap = dict([
    (int(x[0]), x[1:])
    for x in qnums[:10]
])
del x

qmandigit = [
    [u"兆", 12],
    [u"億", 8],
    [u"万", 4],
    [u"千", 3],
    [u"百", 2],
    [u"十", 1],
    [u"z", 0],  # 番人
]

jkpattern = ur"([" + maybenums1 + "".join([z[0] for z in qmandigit[:-1]]) + "]+)"
jkp = re.compile(jkpattern, flags=re.LOCALE)
del maybenums1, jkpattern

qmanany = re.compile(r"(\d*)(\D)")
qmandict = dict(qmandigit)


def replace_jpnumeric_int(src):
    while True:
        qk = jkp.search(src)
        if qk == None:
            break
        rsrc = qk.group(1)
        result = to_int(rsrc)
        if str(result) == rsrc:
            break
        src = src.replace(rsrc, str(result))
    return src


def to_int(src):
    # src = src
    for qmap in qnums:  # 和数字を先にある程度数字に変換
        for q1 in qmap[1:]:
            src = src.replace(q1, qmap[0])
        del q1
    del qmap
    total = 0  # 合計
    ototal = 0  # 万進法未満の値
    for qk in qmanany.finditer(src + qmandigit[-1][0]):  # 単位文字で分割
        pick = qk.group(1)  # 今回の数字
        jkn = qmandict[qk.group(2)]  # の桁

        if jkn == 0 and pick == "":
            break

        if pick == "":
            if ototal == 0: # 1省略に雑に対応
                pick = "1"
            else:
                pick = "0"

        if jkn < 4:
            ototal += int(pick) * (10 ** jkn)
        else:
            ototal += int(pick)
            total += ototal * (10 ** jkn)
            ototal = 0
        pass
    total += ototal  # 万未満の残り
    return total