pytoml
pytoml is a Python library designed for parsing and writing TOML (Tom's Obvious, Minimal Language) files. It specifically targets version 0.4.0 of the TOML specification. The library provides an interface similar to Python's standard `json` module, offering `load`, `loads`, `dump`, and `dumps` functions. The project is no longer actively maintained, with its last release (0.1.21) in July 2019.
Warnings
- breaking The `pytoml` library is officially deprecated and no longer actively maintained. Its GitHub README explicitly advises users to consider alternatives. This means it will not receive updates for new Python versions, TOML specification changes, or security fixes.
- breaking pytoml only supports TOML specification version 0.4.0. The current stable TOML specification is 1.0.0. Using `pytoml` with modern `pyproject.toml` files or other TOML 1.0.0 compliant configurations will likely lead to parsing errors or incorrect data interpretation due to syntax changes between versions.
- gotcha When using `toml.load()`, the file object must be opened in binary read mode (`'rb'`). Passing a text-mode file object (`'r'`) will result in a `TypeError` or unexpected behavior.
Install
-
pip install pytoml
Imports
- load, loads, dump, dumps
import pytoml as toml
Quickstart
import pytoml as toml
import io
# Loading from a string
toml_string = 'title = "My TOML Document"\nowner = { name = "John Doe" }'
data_from_string = toml.loads(toml_string)
print(f"From string: {data_from_string}")
# Loading from a file-like object (e.g., a file)
# Using io.BytesIO to simulate reading a file in binary mode ('rb')
toml_file_content = b'[database]\nserver = "192.168.1.1"\nports = [8001, 8002, 8003]'
file_like_object = io.BytesIO(toml_file_content)
data_from_file = toml.load(file_like_object)
print(f"From file-like object: {data_from_file}")
# Dumping to a string
dict_to_dump = {'server': '10.0.0.1', 'connection_max': 5000}
dumped_string = toml.dumps(dict_to_dump)
print(f"Dumped TOML string:\n{dumped_string}")