LoginSignup
1
1

More than 3 years have passed since last update.

「10」を「ten」という文字に変換する

Posted at

例えばこのテキストを

I have 2 apples.

これに変換したいとする

I have two apples.

そういう場合こちらのライブラリが便利だった
Github - num2words

こんな感じ

from num2words import num2words

text = "I have 2 apples."
print(text)
# I have 2 apples.
new_text = ""
split_words = [num2words(word) if word.isdecimal() else word for word in text.split()]
print(" ".join(split_words))
# I have two apples.

上記コードは下記GoogleColabratoryで再現できるようにしておいた。
GoogleColabratory - num2words.ipynb

参考

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1