40 lines
1.0 KiB
Python
Raw Normal View History

2024-10-25 14:00:31 +08:00
import re
def format_amount(original_amount):
"""
格式化金额字符串
参数:
extracted_text (str): 输入的文本字符串
返回:
str: 格式化后的金额字符串
"""
# amount_str = "未解析到招标项目预算"
# 1. 删除所有空格
amount_str = ''.join(original_amount.split())
# 匹配"万元"模式
wan_pattern = r"(\d+\.?\d*)\s*万元"
wan_match = re.search(wan_pattern, amount_str)
# 匹配"元"模式
yuan_pattern = r"(\d+\.?\d*)\s*元"
yuan_match = re.search(yuan_pattern, amount_str)
if wan_match:
# 如果找到"万元",将其乘以10000并格式化
value = float(wan_match.group(1)) * 10000
amount_str = "{:.0f}".format(value)
elif yuan_match:
# 找到"元",直接获取该数字并格式化
value = float(yuan_match.group(1))
amount_str = "{:.0f}".format(value)
return amount_str
input_test=["RMB100.01万元威威"]
for i in input_test:
formatted=format_amount(i)
print(formatted)