Python實(shí)現(xiàn)中文大寫(xiě)金額轉(zhuǎn)阿拉伯?dāng)?shù)字
在財(cái)務(wù)票據(jù)中,中文大寫(xiě)金額(如“貳拾捌萬(wàn)壹仟柒佰伍拾伍元壹角玖分”)被廣泛使用以防止篡改。但在數(shù)據(jù)處理時(shí),我們需要將其轉(zhuǎn)換為阿拉伯?dāng)?shù)字形式。本文將帶你一步步解析如何用Python實(shí)現(xiàn)這一轉(zhuǎn)換。
一、核心思路拆解
整個(gè)轉(zhuǎn)換過(guò)程可分為三步:
- 中文數(shù)字解析 :將“貳拾捌”等字符串轉(zhuǎn)換為數(shù)字
- 單位分割 :處理“億”、“萬(wàn)”等大單位分隔
- 元角分處理 :分離并轉(zhuǎn)換金額中的整數(shù)與小數(shù)部分
二、中文數(shù)字解析實(shí)現(xiàn)
def chinese_to_number(chinese_str):
result = 0
temp = 0
unit = 1
for char in chinese_str:
if char in chinese_to_arabic:
temp += chinese_to_arabic[char] * unit
if unit < 10:
unit = 1 # 重置單位為個(gè)位
elif char in unit_map:
unit = unit_map[char]
temp = 1 if temp == 0 else temp # 處理"拾"等單位前無(wú)數(shù)字的情況
result += temp * unit
temp = 0
unit = 1
return result + temp解析邏輯 :
- 遍歷每個(gè)字符,遇到數(shù)字字符(壹、貳等)時(shí)累加到臨時(shí)變量
- 遇到單位字符(拾、佰等)時(shí),將當(dāng)前累加值乘以單位后加入結(jié)果
- 特殊處理連續(xù)單位(如"拾萬(wàn)"自動(dòng)補(bǔ)1)
三、大單位分割策略
def extract_parts_yi_wan(s):
result = {"億": "", "萬(wàn)": "", "一": ""}
if "億" in s:
yi_index = s.index("億")
result["億"] = s[:yi_index]
after_yi = s[yi_index+1:]
if "萬(wàn)" in after_yi:
wan_index = after_yi.index("萬(wàn)")
result["萬(wàn)"] = after_yi[:wan_index]
result["一"] = after_yi[wan_index+1:]
else:
result["一"] = after_yi
elif "萬(wàn)" in s:
wan_index = s.index("萬(wàn)")
result["萬(wàn)"] = s[:wan_index]
result["一"] = s[wan_index+1:]
else:
result["一"] = s
return result分割規(guī)則 :
- 優(yōu)先分割"億"級(jí)單位
- 在億級(jí)后的部分繼續(xù)分割"萬(wàn)"級(jí)
- 剩余部分作為個(gè)位部分處理
例如:"貳億叁仟萬(wàn)肆仟伍佰"會(huì)被分割為:
億:"貳"
萬(wàn):"叁仟"
一:"肆仟伍佰"
四、元角分綜合處理
def convert_amounts(amount_list):
pattern = re.compile(
r'([零壹貳叁肆伍陸柒捌玖拾佰仟萬(wàn)億]+)元?'
r'((?:零|[壹貳叁肆伍陸柒捌玖]+)角)?'
r'((?:零|[壹貳叁肆伍陸柒捌玖]+)分)?'
)
converted = []
for amount in amount_list:
match = pattern.findall(amount)
if not match:
converted.append("無(wú)法轉(zhuǎn)換")
continue
yuan, jiao, fen = match[0]
parts = extract_parts_yi_wan(yuan)
total = 0
# 處理億、萬(wàn)、個(gè)位部分
for unit, value in parts.items():
if not value:
continue
num = chinese_to_number(value)
if unit == "億":
num *= 100000000
elif unit == "萬(wàn)":
num *= 10000
total += num
# 處理角分
if jiao and jiao != '零角':
total += chinese_to_arabic[jiao[:-1]] * 0.1
if fen and fen != '零分':
total += chinese_to_arabic[fen[:-1]] * 0.01
converted.append(f"{total:.2f}")
return converted處理流程 :
- 正則提取元、角、分三部分
- 分別解析各部分?jǐn)?shù)值
- 按單位權(quán)重累加計(jì)算總金額
- 保留兩位小數(shù)輸出
五、測(cè)試驗(yàn)證
amounts = [
'貳拾捌萬(wàn)壹仟柒佰伍拾伍元壹角玖分',
'貳拾伍萬(wàn)捌仟肆佰玖拾壹元'
]
print(convert_amounts(amounts)) 輸出結(jié)果:
['281755.19', '258491.00']
六、全部代碼
import re
# 定義中文數(shù)字到阿拉伯?dāng)?shù)字的映射
chinese_to_arabic = {
'零': 0, '壹': 1, '貳': 2, '叁': 3, '肆': 4,
'伍': 5, '陸': 6, '柒': 7, '捌': 8, '玖': 9,
}
unit_map = {'拾': 10, '佰': 100, '仟': 1000}
def chinese_to_number(chinese_str):
result = 0
temp_result = 0
unit = 1
for char in chinese_str:
if char in chinese_to_arabic:
temp_result += chinese_to_arabic[char] * unit
if unit < 10: # 當(dāng)前處理的是個(gè)位數(shù),需要重置unit
unit = 1
elif char in unit_map:
unit = unit_map[char]
temp_result = 1 if temp_result == 0 else temp_result
result += temp_result * unit
temp_result = 0
unit = 1
elif char in ['元', '角', '分']:
break
result += temp_result
return result
def extract_parts_yi_wan(s):
# 初始化結(jié)果字典
result = {"億": "", "萬(wàn)": "", "一": ""}
# 檢查字符串是否包含“億”
if "億" in s:
yi_index = s.index("億")
result["億"] = s[:yi_index]
# 檢查億后面的部分是否包含“萬(wàn)”
after_yi = s[yi_index + 1:]
if "萬(wàn)" in after_yi:
wan_index = after_yi.index("萬(wàn)")
result["萬(wàn)"] = after_yi[:wan_index]
result["一"] = after_yi[wan_index + 1:]
else:
# 如果沒(méi)有“萬(wàn)”,則億與萬(wàn)中間的字符串為空,萬(wàn)后面的字符串為億后面的所有內(nèi)容
result["萬(wàn)"] = ""
result["一"] = after_yi
else:
# 如果沒(méi)有“億”,檢查是否包含“萬(wàn)”
if "萬(wàn)" in s:
wan_index = s.index("萬(wàn)")
result["一"] = s[wan_index + 1:]
result["萬(wàn)"] = s[:wan_index]
else:
# 如果沒(méi)有“萬(wàn)”,則萬(wàn)后面的字符串為空,億前面的字符串為整個(gè)字符串
result["一"] = s
result["萬(wàn)"] = ""
return result
def convert_amounts(amount_list):
pattern = re.compile(
r'([壹貳叁肆伍陸柒捌玖零拾佰仟萬(wàn)億]+)元?((?:零|[壹貳叁肆伍陸柒捌玖]+)角)?((?:零|[壹貳叁肆伍陸柒捌玖]+)分)?')
converted_list = []
for amount in amount_list:
number = 0
match = pattern.findall(amount)
if match:
yuan, jiao, fen = match[0]
ret_yuan = extract_parts_yi_wan(yuan)
for k, v in ret_yuan.items():
new_number = 0
if v:
new_number = chinese_to_number(v)
if k == "億":
new_number *= 100000000
elif k == "萬(wàn)":
new_number *= 10000
number += new_number
if jiao and jiao != '零角':
number += chinese_to_arabic[jiao.replace('角', '')] * 0.1
if fen and fen != '零分':
number += chinese_to_arabic[fen.replace('分', '')] * 0.01
converted_list.append(f"{number:.2f}")
else:
converted_list.append("無(wú)法轉(zhuǎn)換")
return converted_list
amounts = ['貳拾捌萬(wàn)壹仟柒佰伍拾伍元壹角玖分', '貳拾伍萬(wàn)捌仟肆佰玖拾壹元']
converted_amounts = convert_amounts(amounts)
print(converted_amounts)
以上就是Python實(shí)現(xiàn)中文大寫(xiě)金額轉(zhuǎn)阿拉伯?dāng)?shù)字的詳細(xì)內(nèi)容,更多關(guān)于Python中文大寫(xiě)轉(zhuǎn)阿拉伯?dāng)?shù)字的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- 如何使用Python實(shí)現(xiàn)阿拉伯?dāng)?shù)字轉(zhuǎn)換成中國(guó)漢字
- python將中文數(shù)字轉(zhuǎn)化成阿拉伯?dāng)?shù)字的簡(jiǎn)單方法
- Python簡(jiǎn)單實(shí)現(xiàn)阿拉伯?dāng)?shù)字和羅馬數(shù)字的互相轉(zhuǎn)換功能示例
- Python實(shí)現(xiàn)中文數(shù)字轉(zhuǎn)換為阿拉伯?dāng)?shù)字的方法示例
- Python實(shí)現(xiàn)將羅馬數(shù)字轉(zhuǎn)換成普通阿拉伯?dāng)?shù)字的方法
- Python將阿拉伯?dāng)?shù)字轉(zhuǎn)換為羅馬數(shù)字的方法
- python中將阿拉伯?dāng)?shù)字轉(zhuǎn)換成中文的實(shí)現(xiàn)代碼
相關(guān)文章
python opencv實(shí)現(xiàn)圖像矯正功能
這篇文章主要為大家詳細(xì)介紹了python opencv實(shí)現(xiàn)圖像矯正功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
python導(dǎo)包的幾種方法(自定義包的生成以及導(dǎo)入詳解)
這篇文章主要介紹了python導(dǎo)包的幾種方法(自定義包的生成以及導(dǎo)入詳解),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
在VScode中配置Python開(kāi)發(fā)環(huán)境的超詳細(xì)指南
在使用VSCode編寫(xiě)Python代碼前,我們需要先配置Python環(huán)境,這篇文章主要給大家介紹了關(guān)于在VScode中配置Python開(kāi)發(fā)環(huán)境的相關(guān)資料,需要的朋友可以參考下2023-12-12
使用Python實(shí)現(xiàn)自動(dòng)化整理文件的詳細(xì)教程
在日常工作中,我們經(jīng)常需要處理大量的文件,手動(dòng)完成這些任務(wù)不僅耗時(shí),而且容易出錯(cuò),所以下面小編就來(lái)和大家詳細(xì)講講如何使用Python實(shí)現(xiàn)自動(dòng)化整理文件吧2025-08-08
在python tkinter中Canvas實(shí)現(xiàn)進(jìn)度條顯示的方法
今天小編就為大家分享一篇在python tkinter中Canvas實(shí)現(xiàn)進(jìn)度條顯示的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06
Python3中編碼與解碼之Unicode與bytes的講解
今天小編就為大家分享一篇關(guān)于Python3中編碼與解碼之Unicode與bytes的講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-02-02

