Horje
Python word2number Module

Converting words to numbers becomes a quite challenging task in any programming language. Python’s Word2Number module emerges as a handy solution, facilitating the conversion of words denoting numbers into their corresponding numerical values with ease. In this article, we will see what is the word2number module with the help of some examples.

What is the word2number Module in Python?

Python’s Word2Number module serves as a valuable tool for effortlessly converting textual representations of numbers into their numerical equivalents. By offering a straightforward interface and robust functionality, it simplifies the task of processing numeric data expressed in words within Python programs.

Python word2number Module Examples

Below are some examples of the word2number module usage in Python:

Converting Words to Numbers

In this example, the word2number library is used to convert written-out numbers into their numerical equivalents. Specifically, it demonstrates conversion for three examples: “two hundred seventy-five”, “one thousand five hundred twenty-four”, and “one million five hundred twenty-four”.

Python3
from word2number import w2n

# Convert words to numbers
print(w2n.word_to_num("two hundred seventy-five"))
print(w2n.word_to_num("one thousand five hundred twenty-four"))
print(w2n.word_to_num("one million five hundred twenty-four"))

Output:

275 
1524 
1000524

Convert Word to Number in a Pandas DataFrame

In this example, a Pandas DataFrame is created with a column of number words. Utilizing the apply function along with word_to_num from the word2number library, the number words in the DataFrame are converted into numerical values, and a new column is appended with these numerical values. The resulting DataFrame is then printed.

Python3
import pandas as pd
from word2number import w2n

# Create a DataFrame with a column of number words
data = {'Number Words': ['five hundred', 'sixty two', 'three thousand']}
df = pd.DataFrame(data)

# Convert number words to numbers using apply and word_to_num
df['Numerical Values'] = df['Number Words'].apply(w2n.word_to_num)
print(df)

Output:

     Number Words  Numerical Values
0    five hundred               500
1       sixty two                62
2  three thousand              3000



Reffered: https://www.geeksforgeeks.org


Python

Related
File Locking in Python File Locking in Python
Using Espeak With Os In Python Using Espeak With Os In Python
How to Fix - UnboundLocalError: Local variable Referenced Before Assignment in Python How to Fix - UnboundLocalError: Local variable Referenced Before Assignment in Python
How to Find the Index for a Given Item in a Python List How to Find the Index for a Given Item in a Python List
How to Fix - SyntaxError: (Unicode Error) 'Unicodeescape' Codec Can't Decode Bytes How to Fix - SyntaxError: (Unicode Error) 'Unicodeescape' Codec Can't Decode Bytes

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
12