Skip to main content

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)}

Metadata

Release files for parse-formula 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for parse-formula 1.0.0
File Size Uploaded
parse_formula-1.0.0.tar.gz 3.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for parse-formula 1.0.0
File Interpreter ABI Platform
parse_formula-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 7.2 kB

Release files / parse_formula-1.0.0.tar.gz

Download URL parse_formula-1.0.0.tar.gz
Size 3.2 kB
Tags Source
SHA-256 checksum
How to use checksums
febf25e8b7cca10021412dcf306234e3f049c4bd8fc277337a176f580fbe5694
BLAKE2b-256 checksum
How to use checksums
02167281e56b6c57b6d5ac189059b3f78cac6583e8f3f680cbce6673f45a1ae7
Upload date
Uploaded using Trusted Publishing?
What is 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}

Release files / parse_formula-1.0.0-py3-none-any.whl

Download URL parse_formula-1.0.0-py3-none-any.whl
Size 4.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6759fc633b85758639058adaefaac91326a576fe7a6d79c6600cbfb92c429d89
BLAKE2b-256 checksum
How to use checksums
084bf1554ea82dd64995e76119b0e25e31c5f2140330df82d1857c0a96fc01fc
Upload date
Uploaded using Trusted Publishing?
What is 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}

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page