【问题标题】:converting digits to word in string in python在python中将数字转换为字符串中的单词
【发布时间】:2020-11-21 00:50:06
【问题描述】:
> #!/usr/bin/env python
> 
> import inflect
> 
> p = inflect.engine() 
s1 = ''
> 
>         word = 'so do 1st or do 2nd or 3rd byte 4 th so do 5th longest word 3765 word 50 but 7th'
>     list_word = list(word)
>     print (list_word)
>     
>     for(m= 0; m <list_word.len(); m++):
>            if list_word[m].isalpha():
>                    s1 += i + ''
>            elif list_word[m].isnumeric():
>                    if (list_word[m+1].isnumeric()):
>                            continue
>                    elif (list_word[m+1]+list_word[m+2] == 'st'):
>                            s1 += first + ''
>                            m += 2
>                    elif (list_word[m+1]+list_word[m+2] == 'nd'):
>                            s1 += second + ''
>                            m += 2
>                    elif (list_word[m+1]+list_word[m+2] == 'rd'):
>                            s1 += third + ''
>                            m += 2
>                    elif (list_word[m+1]+list_word[m+2] == 'th'):
>                            if (list_word[m] == '5'):
>                                  s1 += fifth + ''
>                                  m += 2
>                            else :
>                                  s1 += p.number_to_words(list_word[m]) + ''
>                                  m += 2
>                    elif (list_word[m+1].isnumeric()):
>                            continue
>                    else:
>                            s1 += p.number_to_words(list_word[m]) + ''
>            else:
>                    s1 += ' '
> 

我需要将数字转换为单词并仅包含字母组成一个完整的字符串。但问题是这种 for 循环 不能在 Python 中使用,我需要在某些地方迭代到下一个字符。请给我一些关于这方面的建议。 或者如果有人可以建议任何其他方法。

【问题讨论】:

  • 也许是for m in list_word:?或者如果您需要索引值,for m in range(len(list_word)):
  • 我试过了,但看起来我的代码变得更复杂了。

标签: python for-loop scripting alphanumeric


【解决方案1】:

最简单的方法是使用 while 循环并手动迭代变量。例如:

m = 0
while m < len(list_word):
   body
   final else
      
   m += 1

实际上这和你使用 for 循环是一样的

【讨论】:

    【解决方案2】:

    使用num2words 包,我们可以这样做来翻译整个字符串而无需任何for循环:

    import re
    import num2words
    
    def transInt(num, mode=None):
        # Changes the mode of the translation to 
        # correctly translate ordinal numbers.
        if mode:
            mode = "ordinal"
        else:
            mode = "cardinal"
    
        # translates the number to either float
        # or int, so the translation works correctly.
        if "." in num:
            num = float(num)
        else:
            num = int(num)
        return num2words.num2words(num, to=mode)
    
    def replaceInt(string):
        # Matches numbers, followed by optional ordinal-characters after it.
        return re.sub(r"(\d+\.*\d*)(st|nd|rd|th)*", lambda x: transInt(*x.groups()), string)
    
    word = 'so do 1st or do 2nd or 3rd byte 4th so do 5th longest word 3765 word 50 but 7th'
    print(word)
    print(replaceInt(word))
    

    输出:

    so do 1st or do 2nd or 3rd byte 4th so do 5th longest word 3765 word 50 but 7th
    so do first or do second or third byte fourth so do fifth longest word three thousand, seven hundred and sixty-five word fifty but seventh
    

    翻译的重点是后缀(5th中的“th”)与数字相连。在您的示例中,您有 "4 th" 不适用于我的代码。或者更确切地说,它将4 翻译为four 而不是fourth

    【讨论】:

    • 谢谢您,先生,它运行良好。我将进一步探索这种方法,因为它似乎非常有用
    猜你喜欢
    • 2016-02-04
    • 2021-02-11
    • 1970-01-01
    • 2017-09-12
    • 2011-06-08
    • 2015-01-03
    • 1970-01-01
    • 2018-01-05
    • 2020-04-02
    相关资源
    最近更新 更多