Skip to content Skip to sidebar Skip to footer

How To Convert Numeric Words Into Numeric In Python

I want to convert numeric which represented in words into numbers. for example, thirty four thousand four fifty into its corresponding numerical value 34450. Also have some fuz

Solution 1:

For numbers to words, try "num2words" package: https://pypi.python.org/pypi/num2words

For words to num, I tweaked the code slightly from the code here: Is there a way to convert number words to Integers?

from num2words import num2words

deftext2int(textnum, numwords={}):
    ifnot numwords:
      units = [
        "zero", "one", "two", "three", "four", "five", "six", "seven", "eight",
        "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
        "sixteen", "seventeen", "eighteen", "nineteen",
      ]

      tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]

      scales = ["hundred", "thousand", "million", "billion", "trillion"]

      numwords["and"] = (1, 0)
      for idx, word inenumerate(units):    numwords[word] = (1, idx)
      for idx, word inenumerate(tens):     numwords[word] = (1, idx * 10)
      for idx, word inenumerate(scales):   numwords[word] = (10 ** (idx * 3or2), 0)

    current = result = 0for word in textnum.split():
        if word notin numwords:
          raise Exception("Illegal word: " + word)

        scale, increment = numwords[word]
        current = current * scale + increment
        if scale > 100:
            result += current
            current = 0return result + current

#### My update to incorporate decimals
num = 5000222223.28
fullText = num2words(num).replace('-',' ').replace(',',' ')
print fullText

decimalSplit = fullText.split('point ')

iflen(decimalSplit) > 1:
    decimalSplit2 = decimalSplit[1].split(' ')
    decPart = sum([float(text2int(decimalSplit2[x]))/(10)**(x+1) for x inrange(len(decimalSplit2))])
else:
    decPart = 0

intPart = float(text2int(decimalSplit[0]))

Value = intPart + decPart

print Value

-> five billion two hundred and twenty two thousand two hundred and twenty three point two eight

-> 5000222223.28

Post a Comment for "How To Convert Numeric Words Into Numeric In Python"