Skip to main content

Chemical formula parser for Python

Project description

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

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

parse_formula-0.1.0.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

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

parse_formula-0.1.0-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file parse_formula-0.1.0.tar.gz.

File metadata

  • Download URL: parse_formula-0.1.0.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.22 {"installer":{"name":"uv","version":"0.9.22","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

Hashes for parse_formula-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8ff3d0beeb6cca7da6da5b05ae7cf81f7d6b84ef09528812af7425bf60df4edb
MD5 a4d98bae4803aef93c13cf504de515cd
BLAKE2b-256 c66d2f05ff7e4fecdb3373b3e987cb9f8820a2f4094f8ab119d6e83e0ff9f2b9

See more details on using hashes here.

File details

Details for the file parse_formula-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: parse_formula-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.22 {"installer":{"name":"uv","version":"0.9.22","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

Hashes for parse_formula-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bceaaddbe0583adb0085ca001954c4b1d43af79633ad9d2b28dc71c8d830413e
MD5 bc3aac3ace4345975be8694514c106d6
BLAKE2b-256 3a41cb320eea09cbebeff2d23833d665900344d72c7199d515f77ba4079ca660

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