#coding=utf-8
#By Gin
#2020/7/17
#Rev 02
import os
import re
ch_dict = {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'两':2,
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 converion(temp, unit):
intr = 0
#print(temp, unit)
temp_len = len(temp)
if temp_len <= 2 and temp[0] == 10: #十到十九需要单独转换
for i in temp:
intr += i
return intr * unit
for i in range(temp_len):
if temp[i] > 9 and temp[i-1] != 0:
intr += temp[i] * temp[i-1]
if temp[-1] < 10: intr += temp[-1]
return intr * unit
def split_unit(temp, unit):
nu = []
while True:
if temp[0] != unit:
nu.append(temp.pop(0))
continue
temp.pop(0)
return converion(nu, unit)
def onere(strarr):
#一一四 ===>>> 114
#四百三十六 ===>>> 436
res = ''
intr = 0
com = re.compile('[十百千万]')
if re.findall(com, strarr):
temp = [ch_dict[i] for i in strarr] #列表666[6, 100, 6, 10, 6]
if 10**8 in temp:
intr += split_unit(temp, 10**8)
if 10**4 in temp:
intr += split_unit(temp, 10**4)
if temp:
intr += converion([ i for i in temp], 1)
return str(intr)
else:
for i in strarr:
res += str(ch_dict[i])
return res
ch_key = ''.join(ch_dict.keys())
com = re.compile('第([' + ch_key + ']{1,20})[章|节]')
file = [] #文件列表
print('更改章节名中的中文数字为阿拉伯数字!')
while True:
try:
# pa = input('请输入路径:')
pa = r'D:\GIN\py\novel\biquge'
os.chdir(pa)
print('当前工作路径为:{}'.format(os.getcwd()))
break
except BaseException as err:
print(err)
continue
for i,j,k in os.walk(pa):
file = k
j.clear()
for i in file:
res = re.findall(com,i)
if res != [''] and res:
news = i.replace(*res, onere(*res))
print('{:<30}\n{:<30}\n'.format(i, news))
# os.rename(i, news)