如何使用 Python 將數字轉換為英文單詞?
Python 中字串類的建構函式(即 str)可用於將數字轉換為 Python 中的字串。例如,
i = 10050 str_i = str(i) print(type(str_i))
這將產生以下輸出
<class 'str'>
但是如果你想將整數轉換單詞,例如將 99 轉換為九十九,則必須使用外部程式包或自己構建一個程式包。pynum2word 模組非常適合這項任務。你可以使用以下命令進行安裝
$ pip install pynum2word
然後以以下方式使用它
>>> import num2word >>> num2word.to_card(16) 'sixteen' >>> num2word.to_card(23) 'twenty-three' >>> num2word.to_card(1223) 'one thousand, two hundred and twenty-three'
廣告