📦 快速转化「中文数字」和「阿拉伯数字」~ (最新特性:分数,日期、温度等转化)
See the code📦 cn2an 是一个快速转化 中文数字 和 阿拉伯数字 的工具包!
🎈
v0.5.24 update: fix numeral parsing and transform edge cases
中文数字 => 阿拉伯数字中文数字 => 阿拉伯数字;大写中文数字 => 阿拉伯数字;中文数字和阿拉伯数字 => 阿拉伯数字;中文数字 => 阿拉伯数字字符串 的原样直译;阿拉伯数字 => 中文数字阿拉伯数字 => 中文数字;阿拉伯数字 => 大写中文数字;阿拉伯数字 => 大写人民币;阿拉伯数字字符串 => 中文数字 的原样直译;支持 中文数字 => 阿拉伯数字;
日期;分数;百分比;摄氏度;支持 阿拉伯数字 => 中文数字;
日期;分数;百分比;摄氏度;小数;负数;HTTP API。⚠️ 注意:
- 本地安装仅支持 Python 的 3.7 以上版本;
- 尽可能使用
cn2an的最新版本。
pip install cn2an -U
git clone https://github.com/Ailln/cn2an.git
cd cn2an && python setup.py install
# 在文件首部引入包
import cn2an
# 查看当前版本号
print(cn2an.__version__)
# 0.5.24
中文数字 => 阿拉伯数字最大支持到
10**16,即千万亿,最小支持到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
# 在 direct 模式下,只做逐位原样转化,并返回字符串
output = cn2an.cn2an("零零三", "direct")
# output:
# 003
output = cn2an.cn2an("一二点三零", "direct")
# output:
# 12.30
阿拉伯数字 => 中文数字最大支持到
10**16,即千万亿,最小支持到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:
# 一点二三
# 在 direct 模式下,只做逐位原样转化
output = cn2an.an2cn("012", "direct")
# output:
# 零一二
output = cn2an.an2cn("12.30", "direct")
# output:
# 一二点三零
⚠️:试验性功能,可能会造成不符合期望的转化。
import cn2an
# 在 cn2an 方法(默认)下,可以将句子中的中文数字转成阿拉伯数字
output = cn2an.transform("小王捡了一百块钱")
# 或者
output = cn2an.transform("小王捡了一百块钱", "cn2an")
# output:
# 小王捡了100块钱
# 在 an2cn 方法下,可以将句子中的中文数字转成阿拉伯数字
output = cn2an.transform("小王捡了100块钱", "an2cn")
# output:
# 小王捡了一百块钱
# direct=True 时,句子中的数字只做逐位原样转化,不做日期、范围等额外处理
output = cn2an.transform("电话零零三,二〇〇二年", "cn2an", direct=True)
# output:
# 电话003,2002年
output = cn2an.transform("电话012,1-2个月", "an2cn", direct=True)
# 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:
# 抛出去的硬币为正面的概率是二分之一
## 支持百分比
## 支持摄氏度
主要为其他语言(Java、Javascript、Go等)用户提供方便,当然 Python 用户也可以使用,点击查看详细用法。
Windows、MacOS、Ubuntu 下的所有 Python 3.7+ 的版本。ubuntu-latest、windows-latest、macOS-latest 的 Python 3.7, 3.9, 3.11 上做过完整测试。Python 2 的使用需求,可 Fork 代码自行修改。当然也欢迎提 PR,贡献自己代码给其他人。本项目是用看板管理开发进度,请点击 v0.5 查看开发进度和计划事项。
本地测试使用 Anaconda 的虚拟环境,测试方法如下:
# 执行测试
bash scripts/local_test.sh
线上测试使用 GitHub Actions 。
测试版本:v0.5.1
测试设备:2.3 GHz 双核Intel Core i5 MacBook Pro
测试代码:performance.py
测试方法:
pip install -r requirements_test.txt
python -m cn2an.performance
测试结果:
| 序号 | 功能 | 执行次数 | 执行时间(万次平均) | 性能(次/秒) |
|---|---|---|---|---|
| 1 | an2cn | 10000 | 0.15 | 67k |
| 2 | cn2an | 10000 | 0.35 | 29k |
测试时,我使用的是最大长度的测试数据!因此,大多数情况下该库的性能会更好~
Overall
Maintained
Code review
PyPI
Latest release
Releases this year
31 followers · starred Apr 2021
103 followers · starred Mar 2025
917 followers · starred Nov 2022
📦 快速转化「中文数字」和「阿拉伯数字」~ (最新特性:分数,日期、温度等转化)
See the code📦 cn2an 是一个快速转化 中文数字 和 阿拉伯数字 的工具包!
🎈
v0.5.24 update: fix numeral parsing and transform edge cases
中文数字 => 阿拉伯数字中文数字 => 阿拉伯数字;大写中文数字 => 阿拉伯数字;中文数字和阿拉伯数字 => 阿拉伯数字;中文数字 => 阿拉伯数字字符串 的原样直译;阿拉伯数字 => 中文数字阿拉伯数字 => 中文数字;阿拉伯数字 => 大写中文数字;阿拉伯数字 => 大写人民币;阿拉伯数字字符串 => 中文数字 的原样直译;支持 中文数字 => 阿拉伯数字;
日期;分数;百分比;摄氏度;支持 阿拉伯数字 => 中文数字;
日期;分数;百分比;摄氏度;小数;负数;HTTP API。⚠️ 注意:
- 本地安装仅支持 Python 的 3.7 以上版本;
- 尽可能使用
cn2an的最新版本。
pip install cn2an -U
git clone https://github.com/Ailln/cn2an.git
cd cn2an && python setup.py install
# 在文件首部引入包
import cn2an
# 查看当前版本号
print(cn2an.__version__)
# 0.5.24
中文数字 => 阿拉伯数字最大支持到
10**16,即千万亿,最小支持到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
# 在 direct 模式下,只做逐位原样转化,并返回字符串
output = cn2an.cn2an("零零三", "direct")
# output:
# 003
output = cn2an.cn2an("一二点三零", "direct")
# output:
# 12.30
阿拉伯数字 => 中文数字最大支持到
10**16,即千万亿,最小支持到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:
# 一点二三
# 在 direct 模式下,只做逐位原样转化
output = cn2an.an2cn("012", "direct")
# output:
# 零一二
output = cn2an.an2cn("12.30", "direct")
# output:
# 一二点三零
⚠️:试验性功能,可能会造成不符合期望的转化。
import cn2an
# 在 cn2an 方法(默认)下,可以将句子中的中文数字转成阿拉伯数字
output = cn2an.transform("小王捡了一百块钱")
# 或者
output = cn2an.transform("小王捡了一百块钱", "cn2an")
# output:
# 小王捡了100块钱
# 在 an2cn 方法下,可以将句子中的中文数字转成阿拉伯数字
output = cn2an.transform("小王捡了100块钱", "an2cn")
# output:
# 小王捡了一百块钱
# direct=True 时,句子中的数字只做逐位原样转化,不做日期、范围等额外处理
output = cn2an.transform("电话零零三,二〇〇二年", "cn2an", direct=True)
# output:
# 电话003,2002年
output = cn2an.transform("电话012,1-2个月", "an2cn", direct=True)
# 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:
# 抛出去的硬币为正面的概率是二分之一
## 支持百分比
## 支持摄氏度
主要为其他语言(Java、Javascript、Go等)用户提供方便,当然 Python 用户也可以使用,点击查看详细用法。
Windows、MacOS、Ubuntu 下的所有 Python 3.7+ 的版本。ubuntu-latest、windows-latest、macOS-latest 的 Python 3.7, 3.9, 3.11 上做过完整测试。Python 2 的使用需求,可 Fork 代码自行修改。当然也欢迎提 PR,贡献自己代码给其他人。本项目是用看板管理开发进度,请点击 v0.5 查看开发进度和计划事项。
本地测试使用 Anaconda 的虚拟环境,测试方法如下:
# 执行测试
bash scripts/local_test.sh
线上测试使用 GitHub Actions 。
测试版本:v0.5.1
测试设备:2.3 GHz 双核Intel Core i5 MacBook Pro
测试代码:performance.py
测试方法:
pip install -r requirements_test.txt
python -m cn2an.performance
测试结果:
| 序号 | 功能 | 执行次数 | 执行时间(万次平均) | 性能(次/秒) |
|---|---|---|---|---|
| 1 | an2cn | 10000 | 0.15 | 67k |
| 2 | cn2an | 10000 | 0.35 | 29k |
测试时,我使用的是最大长度的测试数据!因此,大多数情况下该库的性能会更好~
Overall
Maintained
Code review
PyPI
Latest release
Releases this year
31 followers · starred Apr 2021
103 followers · starred Mar 2025
917 followers · starred Nov 2022