Python Glossary Русский

Python 3.11+tomllibfunction

tomllib.loads()

reading TOML

Parses TOML from a string and returns a dict; use tomllib.load() for files on disk.

Syntax

tomllib.loads(s, /, *, parse_float=float) -> dict

It takes str, not bytes, so decode network data first. parse_float=decimal.Decimal keeps fractional numbers exact, without float rounding. A duplicate key or a redefined table raises TOMLDecodeError rather than silently overwriting, unlike json.loads.

Examples

import tomllib
from decimal import Decimal
doc = 'price = 0.1\ntags = ["x"]'
print(tomllib.loads(doc))  # → {'price': 0.1, 'tags': ['x']}
print(tomllib.loads(doc, parse_float=Decimal)['price'])  # → 0.1
print(type(tomllib.loads(doc, parse_float=Decimal)['price']).__name__)  # → Decimal
import tomllib
try:
    tomllib.loads('a = 1\na = 2')
except tomllib.TOMLDecodeError as e:
    print(type(e).__name__)  # → TOMLDecodeError

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy