py-parse-formula: Chemical formula parser for Python
Installation
pip install parse-formula
Usage
Basic Usage
By default, it returns int counts. (Only accept stoichiometric compounds)
>>> from parse_formula import parse_formula
>>> parse_formula("CH3CH2CH2CH3")
{'C': 4, 'H': 10}
>>> parse_formula("K4[Fe(CN)6]")
{'K': 4, 'Fe': 1, 'C': 6, 'N': 6}
Handling Non-stoichiometric Compounds (float/Decimal/Fraction)
You can specify the type of the returned counts using the parse_number argument.
>>> from decimal import Decimal
>>> from fractions import Fraction
>>> from parse_formula import parse_formula
>>> parse_formula("Fe0.9O")
parse_formula._parser.FormulaValueError: invalid literal for int() with base 10: '0.9'
Hint: Try calling `parse_formula(..., parse_number=float)`
>>> parse_formula("Fe0.9O", parse_number=float)
{'Fe': 0.9, 'O': 1.0}
>>> parse_formula("Fe0.9O", parse_number=Decimal)
{'Fe': Decimal('0.9'), 'O': Decimal('1')}
>>> parse_formula("Fe0.9O", parse_number=Fraction)
{'Fe': Fraction(9, 10), 'O': Fraction(1, 1)}
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
parse_formula-1.0.0.tar.gz
(3.2 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file parse_formula-1.0.0.tar.gz.
File metadata
- Download URL: parse_formula-1.0.0.tar.gz
- Upload date:
- Size: 3.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
febf25e8b7cca10021412dcf306234e3f049c4bd8fc277337a176f580fbe5694
|
|
| MD5 |
278831138b638bd858a757ff9b865a5a
|
|
| BLAKE2b-256 |
02167281e56b6c57b6d5ac189059b3f78cac6583e8f3f680cbce6673f45a1ae7
|
File details
Details for the file parse_formula-1.0.0-py3-none-any.whl.
File metadata
- Download URL: parse_formula-1.0.0-py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6759fc633b85758639058adaefaac91326a576fe7a6d79c6600cbfb92c429d89
|
|
| MD5 |
615d7b1ce2c86934f709b8466180c3fb
|
|
| BLAKE2b-256 |
084bf1554ea82dd64995e76119b0e25e31c5f2140330df82d1857c0a96fc01fc
|