将所有文件名中汉字数字转换成阿拉伯数字

cn2an 是一个快速转化 中文数字 和 阿拉伯数字 的python包

一、cn2an用法

(一)中文数字 => 阿拉伯数字
最大支持到 1016,即 千万亿,最小支持到 10-16

import cn2an
# 在strict模式(默认)下,只有严格符合数字拼写的才可以进行转化
output = cn2an.cn2an("一百二十三")
# 或者
output = cn2an.cn2an("一百二十三", "strict")
# output:
# 123
# 在normal模式下,可以将 一二三 进行转化
output = cn2an.cn2an("一二三", "normal")
# output:
# 123
# 在smart模式下,可以将混合拼写的1百23进行转化
output = cn2an.cn2an("1百23", "smart")
# output:
# 123
# 以上三种模式均支持负数
output = cn2an.cn2an("负一百二十三", "strict")
# output:
# -123
# 以上三种模式均支持小数
output = cn2an.cn2an("一点二三", "strict")
# output:
# 1.23

(二)阿拉伯数字 => 中文数字
最大支持到1016,即千万亿,最小支持到 10-16

import cn2an
# 在low模式(默认)下,数字转化为小写的中文数字
output = cn2an.an2cn("123")
# 或者
output = cn2an.an2cn("123", "low")
# output:
# 一百二十三
# 在up模式下,数字转化为大写的中文数字
output = cn2an.an2cn("123", "up")
# output:
# 壹佰贰拾叁
# 在rmb模式下,数字转化为人民币专用的描述
output = cn2an.an2cn("123", "rmb")
# output:
# 壹佰贰拾叁元整
# 以上三种模式均支持负数
output = cn2an.an2cn("-123", "low")
# output:
# 负一百二十三
# 以上三种模式均支持小数
output = cn2an.an2cn("1.23", "low")
# output:
# 一点二三

(三)句子转化
⚠️:试验性功能,可能会造成不符合期望的转化。

import cn2an
# 在cn2an方法(默认)下,可以将句子中的中文数字转成阿拉伯数字
output = cn2an.transform("小王捡了一百块钱")
# 或者
output = cn2an.transform("小王捡了一百块钱", "cn2an")
# output:
# 小王捡了100块钱
# 在an2cn方法下,可以将句子中的中文数字转成阿拉伯数字
output = cn2an.transform("小王捡了100块钱", "an2cn")
# output:
# 小王捡了一百块钱
## 支持日期
output = cn2an.transform("小王的生日是二零零一年三月四日", "cn2an")
# output:
# 小王的生日是2001年3月4日
output = cn2an.transform("小王的生日是2001年3月4日", "an2cn")
# output:
# 小王的生日是二零零一年三月四日
## 支持分数
output = cn2an.transform("抛出去的硬币为正面的概率是二分之一", "cn2an")
# output:
# 抛出去的硬币为正面的概率是1/2
output = cn2an.transform("抛出去的硬币为正面的概率是1/2", "an2cn")
# output:
# 抛出去的硬币为正面的概率是二分之一

二、实例

将所有文件名中汉字数字转换成阿拉伯数字
第一种方法

import cn2an
import os
import re
ll = os.listdir('.')
print(ll)
regex = re.compile('第([^0-9]*)节')
for ele in ll:
     print(ele)
     matches = regex.findall(ele)
     for i in matches:
         j = cn2an.cn2an(i, 'normal')
         newele = ele.replace(i, str(j))
         print(newele)
         os.rename(ele, newele)

第二种方法

import cn2an
import os
ll = os.listdir('.')
print(ll)
for ele in ll:
     print(ele)
     newele = cn2an.transform(ele, 'cn2an')
     print(newele)
     os.rename(ele, newele)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值