鱼C论坛

 找回密码
 立即注册
查看: 3217|回复: 12

[作品展示] Python把中文数字替换成阿拉伯数字

[复制链接]
发表于 2020-7-15 17:18:10 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 Cool_Breeze 于 2020-7-17 21:41 编辑

更新了! 你们在测试一下看看还有没有bug!
  1. #coding=utf-8
  2. #By Gin
  3. #2020/7/17
  4. #Rev 02

  5. import os
  6. import re

  7. ch_dict = {u'零':0, u'一':1, u'二':2, u'三':3, u'四':4,
  8.         u'五':5, u'六':6, u'七':7, u'八':8, u'九':9,u'十':10,
  9.         u'百':100, u'千':10 ** 3, u'万':10 ** 4,u'〇':0,
  10.         u'壹':1, u'贰':2, u'叁':3, u'肆':4,u'伍':5,
  11.         u'陆':6, u'柒':7, u'捌':8, u'玖':9,u'拾':10,
  12.         u'佰':100, u'仟':10 ** 3, u'萬':10 ** 4,u'亿':10 ** 8, u'億':10 ** 8,
  13.         u'幺': 1,u'两':2,
  14.         u'0':0, u'1':1, u'2':2, u'3':3, u'4':4,
  15.         u'5':5, u'6':6, u'7':7, u'8':8, u'9':9}

  16. def converion(temp, unit):
  17.     intr = 0
  18.     #print(temp, unit)
  19.     temp_len = len(temp)
  20.     if temp_len <= 2 and temp[0] == 10: #十到十九需要单独转换
  21.         for i in temp:
  22.             intr += i
  23.         return intr * unit
  24.     for i in range(temp_len):
  25.         if temp[i] > 9 and temp[i-1] != 0:
  26.             intr += temp[i] * temp[i-1]
  27.     if temp[-1] < 10: intr += temp[-1]
  28.     return intr * unit

  29. def split_unit(temp, unit):
  30.     nu = []
  31.     while True:
  32.         if temp[0] != unit:
  33.             nu.append(temp.pop(0))
  34.             continue
  35.         temp.pop(0)
  36.         return converion(nu, unit)

  37. def onere(strarr):
  38.     #一一四 ===>>> 114
  39.     #四百三十六 ===>>> 436
  40.     res = ''
  41.     intr = 0
  42.     com = re.compile('[十百千万]')
  43.     if re.findall(com, strarr):
  44.         temp = [ch_dict[i] for i in strarr] #列表666[6, 100, 6, 10, 6]
  45.         if 10**8 in temp:
  46.             intr += split_unit(temp, 10**8)

  47.         if 10**4 in temp:
  48.             intr += split_unit(temp, 10**4)
  49.         if temp:
  50.             intr += converion([ i for i in temp], 1)
  51.         return str(intr)
  52.     else:
  53.         for i in strarr:
  54.             res += str(ch_dict[i])
  55.     return res

  56. ch_key = ''.join(ch_dict.keys())
  57. com = re.compile('第([' + ch_key + ']{1,20})[章|节]')
  58. file = [] #文件列表

  59. print('更改章节名中的中文数字为阿拉伯数字!')

  60. while True:
  61.     try:
  62.         # pa = input('请输入路径:')
  63.         pa = r'D:\GIN\py\novel\biquge'
  64.         os.chdir(pa)
  65.         print('当前工作路径为:{}'.format(os.getcwd()))
  66.         break
  67.     except BaseException as err:
  68.         print(err)
  69.         continue

  70. for i,j,k in os.walk(pa):
  71.     file = k
  72.     j.clear()

  73. for i in file:
  74.     res = re.findall(com,i)
  75.     if res != [''] and res:
  76.         news = i.replace(*res, onere(*res))
  77.         print('{:<30}\n{:<30}\n'.format(i, news))
  78.         # os.rename(i, news)
复制代码
  1. 更改章节名中的中文数字为阿拉伯数字!
  2. 当前工作路径为:D:\GIN\py\novel\biquge
  3. 第一万亿两千三百六十五万三千六百二十八章.txt
  4. 第1000023653628章.txt

  5. 第一亿一千一百二十三万四千五百六十七章 - 副本.txt
  6. 第111234567章 - 副本.txt

  7. 第一千一百二十三万四千五百六十七章 - 副本 (9).txt
  8. 第11234567章 - 副本 (9).txt

  9. 第一千二百零三章 - 副本 (5).txt
  10. 第1203章 - 副本 (5).txt

  11. 第一百二十三万四千五百六十七章 - 副本 (8).txt
  12. 第1234567章 - 副本 (8).txt

  13. 第一百二十三章 - 副本 (4).txt
  14. 第123章 - 副本 (4).txt

  15. 第一百零二亿五千零一万零一千零三十八章 - 副本 - 副本.txt
  16. 第10250011038章 - 副本 - 副本.txt

  17. 第八章 - 副本 (3).txt
  18. 第8章 - 副本 (3).txt

  19. 第十一章 - 副本 (2).txt
  20. 第11章 - 副本 (2).txt

  21. 第十万零三千六百零九章 - 副本 (7).txt
  22. 第103609章 - 副本 (7).txt


  23. 请按任意键继续. . .
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-7-16 15:33:53 | 显示全部楼层
沙发
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-7-17 14:23:18 | 显示全部楼层
转换的准吗
比如一万亿两千三百六十五万三千六百二十八
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-17 16:36:40 | 显示全部楼层
本帖最后由 Cool_Breeze 于 2020-7-17 16:38 编辑
nahongyan1997 发表于 2020-7-17 14:23
转换的准吗
比如一万亿两千三百六十五万三千六百二十八


不能,还需要改进,现在只能转换10w以内的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-17 16:52:02 | 显示全部楼层
好的
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-7-17 16:52:59 | 显示全部楼层
这个可以咱俩一起研究,我想弄个闹钟的程序需要这个转换,虽然没有那么大的数但是还是有必要精确的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-17 17:23:31 From FishC Mobile | 显示全部楼层
顶一下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-17 20:02:57 | 显示全部楼层
没有学过数据分析,很是麻烦,大家给点意见吧!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-17 21:22:24 | 显示全部楼层
nahongyan1997 发表于 2020-7-17 16:52
这个可以咱俩一起研究,我想弄个闹钟的程序需要这个转换,虽然没有那么大的数但是还是有必要精确的

更新了!你去试试!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-18 21:32:56 | 显示全部楼层
本帖最后由 Cool_Breeze 于 2020-7-20 10:53 编辑

单一
  1. #coding=utf-8
  2. #By Gin
  3. #2020/7/17
  4. #Rev 02

  5. ch_dict = {u'零':0, u'一':1, u'二':2, u'三':3, u'四':4,
  6.         u'五':5, u'六':6, u'七':7, u'八':8, u'九':9,u'十':10,
  7.         u'百':100, u'千':10 ** 3, u'万':10 ** 4,u'〇':0,
  8.         u'壹':1, u'贰':2, u'叁':3, u'肆':4,u'伍':5,
  9.         u'陆':6, u'柒':7, u'捌':8, u'玖':9,u'拾':10,
  10.         u'佰':100, u'仟':10 ** 3, u'萬':10 ** 4,u'亿':10 ** 8, u'億':10 ** 8,
  11.         u'幺': 1,u'两':2}

  12. def converion(temp, unit):
  13.     intr = 0
  14.     # print(temp, unit)
  15.     temp_len = len(temp)
  16.     if temp_len <= 2 and temp[0] == 10: #十到十九需要单独转换
  17.         for i in temp:
  18.             intr += i
  19.         return intr * unit
  20.     for i in range(temp_len):
  21.         if temp[i] > 9 and temp[i-1] != 0:
  22.             intr += temp[i] * temp[i-1]
  23.     if temp[-1] < 10: intr += temp[-1]
  24.     return intr * unit

  25. def split_unit(temp, unit):
  26.     nu = []
  27.     while True:
  28.         if temp[0] != unit:
  29.             nu.append(temp.pop(0))
  30.             continue
  31.         temp.pop(0)
  32.         return converion(nu, unit)

  33. def onere(strarr):
  34.     intr = 0
  35.     temp = [ch_dict[i] for i in strarr] #列表666[6, 100, 6, 10, 6]
  36.     if 10**8 in temp:
  37.         intr += split_unit(temp, 10**8)
  38.     if 10**4 in temp:
  39.         intr += split_unit(temp, 10**4)
  40.     if temp:
  41.         intr += converion(temp, 1)
  42.     return str(intr)

  43. test_dig = [u'九',
  44.                 u'十一',
  45.                 u'一百二十三',
  46.                 u'一千二百零三',
  47.                 u'一万一千一百零一',
  48.                 u'十万零三千六百零九',
  49.                 u'一百二十三万四千五百六十七',
  50.                 u'一千一百二十三万四千五百六十七',
  51.                 u'一亿一千一百二十三万四千五百六十七',
  52.                 u'一百零二亿五千零一万零一千零三十八',
  53.                 u'一千一百一十一亿一千一百二十三万四千五百六十七',
  54.                 ]
  55.                
  56. for i in test_dig:
  57.     print(i,'\n',onere(i))
复制代码

  1. 9
  2. 十一
  3. 11
  4. 一百二十三
  5. 123
  6. 一千二百零三
  7. 1203
  8. 一万一千一百零一
  9. 11101
  10. 十万零三千六百零九
  11. 103609
  12. 一百二十三万四千五百六十七
  13. 1234567
  14. 一千一百二十三万四千五百六十七
  15. 11234567
  16. 一亿一千一百二十三万四千五百六十七
  17. 111234567
  18. 一百零二亿五千零一万零一千零三十八
  19. 10250011038
  20. 一千一百一十一亿一千一百二十三万四千五百六十七
  21. 111111234567

  22. 请按任意键继续. . .
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-20 17:42:31 | 显示全部楼层
u'一亿亿 '

10000000100000000

发现BUG!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-20 17:43:50 | 显示全部楼层
本帖最后由 nahongyan1997 于 2020-7-20 17:45 编辑

有些数字单位叠加要做乘算还要考虑是不是在4的倍数位上。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-21 12:03:41 | 显示全部楼层
nahongyan1997 发表于 2020-7-20 17:43
有些数字单位叠加要做乘算还要考虑是不是在4的倍数位上。

的确!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-24 00:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表