| Name | price-parser JSON |
| Version |
0.5.0
JSON |
| download |
| home_page | None |
| Summary | Extract price and currency from a raw string |
| upload_time | 2025-10-06 09:52:40 |
| maintainer | None |
| docs_url | None |
| author | None |
| requires_python | >=3.9 |
| license | None |
| keywords |
|
| VCS |
 |
| bugtrack_url |
|
| requirements |
No requirements were recorded.
|
| Travis-CI |
No Travis.
|
| coveralls test coverage |
No coveralls.
|
============
price-parser
============
.. image:: https://img.shields.io/pypi/v/price-parser.svg
:target: https://pypi.python.org/pypi/price-parser
:alt: PyPI Version
.. image:: https://img.shields.io/pypi/pyversions/price-parser.svg
:target: https://pypi.python.org/pypi/price-parser
:alt: Supported Python Versions
.. image:: https://github.com/scrapinghub/price-parser/actions/workflows/main.yml/badge.svg?branch=master
:target: https://github.com/scrapinghub/price-parser/actions?workflow=Tests
:alt: Build Status
.. image:: https://codecov.io/github/scrapinghub/price-parser/coverage.svg?branch=master
:target: https://codecov.io/gh/scrapinghub/price-parser
:alt: Coverage report
``price-parser`` is a small library for extracting price and currency from
raw text strings.
Features:
* robust price amount and currency symbol extraction
* zero-effort handling of thousand and decimal separators
The main use case is parsing prices extracted from web pages.
For example, you can write a CSS/XPath selector which targets an element
with a price, and then use this library for cleaning it up,
instead of writing custom site-specific regex or Python code.
License is BSD 3-clause.
Installation
============
::
pip install price-parser
price-parser requires Python 3.9+.
Usage
=====
Basic usage
-----------
>>> from price_parser import Price
>>> price = Price.fromstring("22,90 €")
>>> price
Price(amount=Decimal('22.90'), currency='€')
>>> price.amount # numeric price amount
Decimal('22.90')
>>> price.currency # currency symbol, as appears in the string
'€'
>>> price.amount_text # price amount, as appears in the string
'22,90'
>>> price.amount_float # price amount as float, not Decimal
22.9
If you prefer, ``Price.fromstring`` has an alias ``price_parser.parse_price``,
they do the same:
>>> from price_parser import parse_price
>>> parse_price("22,90 €")
Price(amount=Decimal('22.90'), currency='€')
The library has extensive tests (900+ real-world examples of price strings).
Some of the supported cases are described below.
Supported cases
---------------
Unclean price strings with various currencies are supported;
thousand separators and decimal separators are handled:
>>> Price.fromstring("Price: $119.00")
Price(amount=Decimal('119.00'), currency='$')
>>> Price.fromstring("15 130 Р")
Price(amount=Decimal('15130'), currency='Р')
>>> Price.fromstring("151,200 تومان")
Price(amount=Decimal('151200'), currency='تومان')
>>> Price.fromstring("Rp 1.550.000")
Price(amount=Decimal('1550000'), currency='Rp')
>>> Price.fromstring("Běžná cena 75 990,00 Kč")
Price(amount=Decimal('75990.00'), currency='Kč')
Euro sign is used as a decimal separator in a wild:
>>> Price.fromstring("1,235€ 99")
Price(amount=Decimal('1235.99'), currency='€')
>>> Price.fromstring("99 € 95 €")
Price(amount=Decimal('99'), currency='€')
>>> Price.fromstring("35€ 999")
Price(amount=Decimal('35'), currency='€')
Some special cases are handled:
>>> Price.fromstring("Free")
Price(amount=Decimal('0'), currency=None)
When price or currency can't be extracted, corresponding
attribute values are set to None:
>>> Price.fromstring("")
Price(amount=None, currency=None)
>>> Price.fromstring("Foo")
Price(amount=None, currency=None)
>>> Price.fromstring("50% OFF")
Price(amount=None, currency=None)
>>> Price.fromstring("50")
Price(amount=Decimal('50'), currency=None)
>>> Price.fromstring("R$")
Price(amount=None, currency='R$')
Currency hints
--------------
``currency_hint`` argument allows to pass a text string which may (or may not)
contain currency information. This feature is most useful for automated price
extraction.
>>> Price.fromstring("34.99", currency_hint="руб. (шт)")
Price(amount=Decimal('34.99'), currency='руб.')
Note that currency mentioned in the main price string may be
**preferred** over currency specified in ``currency_hint`` argument;
it depends on currency symbols found there. If you know the correct currency,
you can set it directly:
>>> price = Price.fromstring("1 000")
>>> price.currency = 'EUR'
>>> price
Price(amount=Decimal('1000'), currency='EUR')
Decimal separator
-----------------
If you know which symbol is used as a decimal separator in the input string,
pass that symbol in the ``decimal_separator`` argument to prevent price-parser
from guessing the wrong decimal separator symbol.
>>> Price.fromstring("Price: $140.600", decimal_separator=".")
Price(amount=Decimal('140.600'), currency='$')
>>> Price.fromstring("Price: $140.600", decimal_separator=",")
Price(amount=Decimal('140600'), currency='$')
Contributing
============
* Source code: https://github.com/scrapinghub/price-parser
* Issue tracker: https://github.com/scrapinghub/price-parser/issues
Use tox_ to run tests with different Python versions::
tox
The command above also runs type checks; we use mypy.
.. _tox: https://tox.readthedocs.io
Raw data
{
"_id": null,
"home_page": null,
"name": "price-parser",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": null,
"keywords": null,
"author": null,
"author_email": "Mikhail Korobov <kmike84@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/71/ad/1dbbe2f5baf98f272f2b557124d95e4c68481ba4e1593b90cb39c5836542/price_parser-0.5.0.tar.gz",
"platform": null,
"description": "============\nprice-parser\n============\n\n.. image:: https://img.shields.io/pypi/v/price-parser.svg\n :target: https://pypi.python.org/pypi/price-parser\n :alt: PyPI Version\n\n.. image:: https://img.shields.io/pypi/pyversions/price-parser.svg\n :target: https://pypi.python.org/pypi/price-parser\n :alt: Supported Python Versions\n\n.. image:: https://github.com/scrapinghub/price-parser/actions/workflows/main.yml/badge.svg?branch=master\n :target: https://github.com/scrapinghub/price-parser/actions?workflow=Tests\n :alt: Build Status\n\n.. image:: https://codecov.io/github/scrapinghub/price-parser/coverage.svg?branch=master\n :target: https://codecov.io/gh/scrapinghub/price-parser\n :alt: Coverage report\n\n\n``price-parser`` is a small library for extracting price and currency from\nraw text strings.\n\nFeatures:\n\n* robust price amount and currency symbol extraction\n* zero-effort handling of thousand and decimal separators\n\nThe main use case is parsing prices extracted from web pages.\nFor example, you can write a CSS/XPath selector which targets an element\nwith a price, and then use this library for cleaning it up,\ninstead of writing custom site-specific regex or Python code.\n\nLicense is BSD 3-clause.\n\nInstallation\n============\n\n::\n\n pip install price-parser\n\nprice-parser requires Python 3.9+.\n\nUsage\n=====\n\nBasic usage\n-----------\n\n>>> from price_parser import Price\n>>> price = Price.fromstring(\"22,90 \u20ac\")\n>>> price\nPrice(amount=Decimal('22.90'), currency='\u20ac')\n>>> price.amount # numeric price amount\nDecimal('22.90')\n>>> price.currency # currency symbol, as appears in the string\n'\u20ac'\n>>> price.amount_text # price amount, as appears in the string\n'22,90'\n>>> price.amount_float # price amount as float, not Decimal\n22.9\n\nIf you prefer, ``Price.fromstring`` has an alias ``price_parser.parse_price``,\nthey do the same:\n\n>>> from price_parser import parse_price\n>>> parse_price(\"22,90 \u20ac\")\nPrice(amount=Decimal('22.90'), currency='\u20ac')\n\nThe library has extensive tests (900+ real-world examples of price strings).\nSome of the supported cases are described below.\n\nSupported cases\n---------------\n\nUnclean price strings with various currencies are supported;\nthousand separators and decimal separators are handled:\n\n>>> Price.fromstring(\"Price: $119.00\")\nPrice(amount=Decimal('119.00'), currency='$')\n\n>>> Price.fromstring(\"15 130 \u0420\")\nPrice(amount=Decimal('15130'), currency='\u0420')\n\n>>> Price.fromstring(\"151,200 \u062a\u0648\u0645\u0627\u0646\")\nPrice(amount=Decimal('151200'), currency='\u062a\u0648\u0645\u0627\u0646')\n\n>>> Price.fromstring(\"Rp 1.550.000\")\nPrice(amount=Decimal('1550000'), currency='Rp')\n\n>>> Price.fromstring(\"B\u011b\u017en\u00e1 cena 75 990,00 K\u010d\")\nPrice(amount=Decimal('75990.00'), currency='K\u010d')\n\n\nEuro sign is used as a decimal separator in a wild:\n\n>>> Price.fromstring(\"1,235\u20ac 99\")\nPrice(amount=Decimal('1235.99'), currency='\u20ac')\n\n>>> Price.fromstring(\"99 \u20ac 95 \u20ac\")\nPrice(amount=Decimal('99'), currency='\u20ac')\n\n>>> Price.fromstring(\"35\u20ac 999\")\nPrice(amount=Decimal('35'), currency='\u20ac')\n\n\nSome special cases are handled:\n\n>>> Price.fromstring(\"Free\")\nPrice(amount=Decimal('0'), currency=None)\n\n\nWhen price or currency can't be extracted, corresponding\nattribute values are set to None:\n\n>>> Price.fromstring(\"\")\nPrice(amount=None, currency=None)\n\n>>> Price.fromstring(\"Foo\")\nPrice(amount=None, currency=None)\n\n>>> Price.fromstring(\"50% OFF\")\nPrice(amount=None, currency=None)\n\n>>> Price.fromstring(\"50\")\nPrice(amount=Decimal('50'), currency=None)\n\n>>> Price.fromstring(\"R$\")\nPrice(amount=None, currency='R$')\n\n\nCurrency hints\n--------------\n\n``currency_hint`` argument allows to pass a text string which may (or may not)\ncontain currency information. This feature is most useful for automated price\nextraction.\n\n>>> Price.fromstring(\"34.99\", currency_hint=\"\u0440\u0443\u0431. (\u0448\u0442)\")\nPrice(amount=Decimal('34.99'), currency='\u0440\u0443\u0431.')\n\nNote that currency mentioned in the main price string may be\n**preferred** over currency specified in ``currency_hint`` argument;\nit depends on currency symbols found there. If you know the correct currency,\nyou can set it directly:\n\n>>> price = Price.fromstring(\"1 000\")\n>>> price.currency = 'EUR'\n>>> price\nPrice(amount=Decimal('1000'), currency='EUR')\n\n\nDecimal separator\n-----------------\n\nIf you know which symbol is used as a decimal separator in the input string,\npass that symbol in the ``decimal_separator`` argument to prevent price-parser\nfrom guessing the wrong decimal separator symbol.\n\n>>> Price.fromstring(\"Price: $140.600\", decimal_separator=\".\")\nPrice(amount=Decimal('140.600'), currency='$')\n\n>>> Price.fromstring(\"Price: $140.600\", decimal_separator=\",\")\nPrice(amount=Decimal('140600'), currency='$')\n\n\nContributing\n============\n\n* Source code: https://github.com/scrapinghub/price-parser\n* Issue tracker: https://github.com/scrapinghub/price-parser/issues\n\nUse tox_ to run tests with different Python versions::\n\n tox\n\nThe command above also runs type checks; we use mypy.\n\n.. _tox: https://tox.readthedocs.io\n",
"bugtrack_url": null,
"license": null,
"summary": "Extract price and currency from a raw string",
"version": "0.5.0",
"project_urls": {
"Homepage": "https://github.com/scrapinghub/price-parser",
"Source": "https://github.com/scrapinghub/price-parser",
"Tracker": "https://github.com/scrapinghub/price-parser/issues"
},
"split_keywords": [],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "7b7309ebcc1650df02390a45ee252af3fbcdd7cb1fd14bd41941b42c273b046a",
"md5": "8a7445b863d62a0df2a686e61d7e9356",
"sha256": "7851375f92e1237ed909110d5a7394e880acb4971b1459dd773ce903ba83b6f0"
},
"downloads": -1,
"filename": "price_parser-0.5.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "8a7445b863d62a0df2a686e61d7e9356",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 14771,
"upload_time": "2025-10-06T09:52:39",
"upload_time_iso_8601": "2025-10-06T09:52:39.642548Z",
"url": "https://files.pythonhosted.org/packages/7b/73/09ebcc1650df02390a45ee252af3fbcdd7cb1fd14bd41941b42c273b046a/price_parser-0.5.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "71ad1dbbe2f5baf98f272f2b557124d95e4c68481ba4e1593b90cb39c5836542",
"md5": "0f2139d99fa6ec2182ea425a35e069f2",
"sha256": "5053af8726de8d412d066f69f721ed5e3e0d61a29f27f6fc3a348d60917ece30"
},
"downloads": -1,
"filename": "price_parser-0.5.0.tar.gz",
"has_sig": false,
"md5_digest": "0f2139d99fa6ec2182ea425a35e069f2",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 34312,
"upload_time": "2025-10-06T09:52:40",
"upload_time_iso_8601": "2025-10-06T09:52:40.664117Z",
"url": "https://files.pythonhosted.org/packages/71/ad/1dbbe2f5baf98f272f2b557124d95e4c68481ba4e1593b90cb39c5836542/price_parser-0.5.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-10-06 09:52:40",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "scrapinghub",
"github_project": "price-parser",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "price-parser"
}