Word2Number
Word2Number is a Python module, currently at version 1.1, designed to convert number words (e.g., 'three hundred and forty two') into their corresponding numeric digits (342). It supports positive numbers up to the billions (999,999,999,999). The library has an irregular release cadence, with updates addressing specific issues and improvements.
Warnings
- breaking Versions prior to 1.0 were primarily compatible with Python 2. Upgrading directly from very old versions to 1.0+ will require migrating Python 2 code to Python 3 syntax and semantics.
- breaking In version 1.1, the library changed its error handling for invalid inputs. Previously, it would print an error message to `stderr` and return `None`. Now, it `Raise`s a `ValueError`. Code that checked for `None` will break.
- gotcha The library is designed for positive numbers and has an upper limit of 999,999,999,999 (billions). It does not support negative numbers or numbers exceeding this range.
- gotcha Input phrases containing redundant number words (e.g., 'million million') will result in an error, as the library expects well-formed number phrases.
Install
-
pip install word2number -
pip install word2number --upgrade
Imports
- w2n
from word2number import w2n
Quickstart
from word2number import w2n
# Basic conversion
print(w2n.word_to_num("two million three thousand nine hundred and eighty four"))
# Handling decimals
print(w2n.word_to_num('two point three'))
# With hyphens
print(w2n.word_to_num('one hundred thirty-five'))
# Direct digits (supported but not primary use-case)
print(w2n.word_to_num('112'))
# Example of an invalid input (will raise ValueError in v1.1+)
try:
print(w2n.word_to_num('blah'))
except ValueError as e:
print(f"Error: {e}")