使用python3快速将一百以内的中文数字(一二三四...)转换为int类型的数字(1234...)

    xiaoxiao2025-01-10  16

    # 传入中文数字 str_num = "二十一" # 一、定义转换字典 num_dict = {"一": "1", "二": "2", "三": "3", "四": "4", "五": "5", "六": "6", "七": "7", "八": "8", "九": "9", "十": ""} # 将中文的数字替换成阿拉伯数字 # 针对不同的情况进行判断和字典的更改 if str_num[0] == "十" and len(str_num) > 1: num_dict["十"] = "1" if str_num[0] == "十" and len(str_num) == 1: num_dict["十"] = "10" if str_num[1] == "十" and len(str_num) == 2: num_dict["十"] = "0" num = "" # 遍历字典并对中文数字进行替换 for str in str_num: for key in num_dict: if key == str: num += num_dict[key] break print(num)

    完毕!

    最新回复(0)