将句子中的数字转换为python中的单词

Say*_*ale 1 python numbers

我有一个文本,我在句子中有一些数字,我只想将数字转换为单词格式。我该如何解决这个问题。我已经为此编写了一个代码,但这不起作用,因为我将文本传递给“函数”而不是数字。我怎么做

我试过下面的代码。

import num2words


def convert_num_to_words(utterance):
      utterance = num2words(utterance)
      return utterance

transcript = "If you can call the merchant and cancelled the transaction and confirm from them that they will not take the payment the funds will automatically be credited back into your account after 24 hours as it will expire on 11/04 Gemma"

print(convert_num_to_words("transcript"))
Run Code Online (Sandbox Code Playgroud)

预期结果是

“如果您可以致电商家并取消交易并从他们那里确认他们不会接受付款,那么资金将在 24 小时后自动记入您的账户,因为它将在 11/04 Gemma 到期”

即文本中的数字 24 应转换为单词(二十四)

U10*_*ard 5

您需要对字符串的每个单词都执行此操作,并且仅当它是数字时,还要删除 旁边的引号transcript,也不要num2words.num2words(...)只是 num2words(...)

import num2words

def convert_num_to_words(utterance):
      utterance = ' '.join([num2words.num2words(i) if i.isdigit() else i for i in utterance.split()])
      return utterance

transcript = "If you can call the merchant and cancelled the transaction and confirm from them that they will not take the payment the funds will automatically be credited back into your account after 24 hours as it will expire on 11/04 Gemma"

print(convert_num_to_words(transcript))
Run Code Online (Sandbox Code Playgroud)