Skip to main content

Extract numbers from strings with support for commas, decimals, negatives, and the European format.

Project description

extract-numbers extract-numbers

Extract numbers from strings, with support for commas, decimals, negatives, and the European format.


Installation

pip install extract-numbers

Or if you're installing from source:

pip install -e .

Usage

from extract_numbers import ExtractNumbers

extractor = ExtractNumbers()

text = "100,000 results found"
extractor.extractNumbers(text)
# => ['100,000']

API

ExtractNumbers(options: dict = None)

Create a new extractor instance with optional config.

.extractNumbers(text: str) -> List[str | int | float]

Extracts all valid numbers from the given string, based on the configured options.

Examples

Basic Extraction

extractor = ExtractNumbers()

extractor.extractNumbers("3030 results found")
# => ['3030']

extractor.extractNumbers("50 out of 100")
# => ['50', '100']

Negatives

extractor.extractNumbers("Temperature: -15°C, yesterday: -22, before: -20.5")
# => ['-15', '-22', '-20.5']

extractor.extractNumbers("-170,000, -222,987 and -222,987,899.70 respectively.")
# => ['-170,000', '-222,987', '-222,987,899.70']

Commas and Decimals

extractor.extractNumbers("100,000 out of 220,000,000 people")
# => ['100,000', '220,000,000']

extractor.extractNumbers("Your rating is 8.7")
# => ['8.7']

extractor.extractNumbers("Your balance: $100,000.77, previous: $90,899.89")
# => ['100,000.77', '90,899.89']

Options

Type: Object

as_string

Type: bool Default: True

If set to False, all numbers are returned as int or float types.

ExtractNumbers({ 'as_string': False }).extractNumbers("7.7, 3030, 90,899,232.89 and -222,987,899.9")
# => [7.7, 3030, 90899232.89, -222987899.9]

remove_commas

Type: bool Default: False Works only when as_string=True.

Removes thousands separators from the returned number strings.

ExtractNumbers({ 'remove_commas': True }).extractNumbers("100,000,000 and -222,987,899.9")
# => ['100000000', '-222987899.9']

european_format

Type: bool Default: False Works only when as_string=True.

Enables European number formatting (e.g., 1.000.000,99 instead of 1,000,000.99).

ExtractNumbers({ 'european_format': True }).extractNumbers("1.000.000,77")
# => ['1.000.000,77']

ExtractNumbers({ 'european_format': True, 'as_string': False }).extractNumbers("1.000.000,77")
# => [1000000.77]

With remove_commas=True:

ExtractNumbers({ 'european_format': True, 'remove_commas': True }).extractNumbers("1.000.000,77")
# => ['1000000,77']

💡 Note: The decimal comma is preserved for as_string=True to support formatting needs. Use as_string=False to convert to a float.


Contributing

Contributions, issues, and feature requests are welcome!

Feel free to:

  • Fork the repo
  • Create a new branch
  • Submit a pull request

If you're fixing a bug or adding a feature, please include a clear description in your PR.

Ensure your changes are covered by tests.


Test Coverage

To run tests:

pytest

License

MIT © Talha Awan

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

extract_numbers-1.0.0.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

extract_numbers-1.0.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file extract_numbers-1.0.0.tar.gz.

File metadata

  • Download URL: extract_numbers-1.0.0.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.5

File hashes

Hashes for extract_numbers-1.0.0.tar.gz
Algorithm Hash digest
SHA256 a0d2408e22e81d9338924563fbe0df083e4b029ab5c7e6b3b3bf048d35279c23
MD5 4f4cb2db9edf719373c27213f06a315f
BLAKE2b-256 f387b64dd05e08caecd9c7a3dcb51a3994e53c3c403517f23e8e09eb2ae45e32

See more details on using hashes here.

File details

Details for the file extract_numbers-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for extract_numbers-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 87df498bb416f480dabbd54fe742d890608d1eb81a7960519310ebff4bef8bc3
MD5 74f1fb9e52f839c18bbbc8baba52eedd
BLAKE2b-256 77bb8999904f1ce2afcf5ee0a16b1dcb6019f89ac0ea6e7c34f06126955db251

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page