tomllib — 剖析 TOML 文件

Added in version 3.11.

源代码: Lib/tomllib


This module provides an interface for parsing TOML 1.0.0 (Tom’s Obvious Minimal Language, https://toml.io )。本模块不支持写入 TOML。

另请参阅

The Tomli-W package is a TOML writer that can be used in conjunction with this module, providing a write API familiar to users of the standard library marshal and pickle 模块。

另请参阅

The TOML Kit package is a style-preserving TOML library with both read and write capability. It is a recommended replacement for this module for editing already existing TOML files.

此模块定义了下列函数:

tomllib. load ( fp , / , * , parse_float = float )

读取 TOML (汤姆的明显最小语言) 文件。第 1 自变量应该是可读的且是二进制文件对象。返回 dict 。把 TOML 类型转换到 Python 使用此 转换表 .

parse_float will be called with the string of every TOML float to be decoded. By default, this is equivalent to float(num_str) . This can be used to use another datatype or parser for TOML floats (e.g. decimal.Decimal )。可调用不得返回 dict list ,否则 ValueError 被引发。

A TOMLDecodeError 会被引发,在无效 TOML (汤姆的明显最小语言) 文档。

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

加载 TOML 从 str 对象。返回 dict 。把 TOML 类型转换到 Python 使用此 转换表 parse_float 自变量拥有相同含义如在 load() .

A TOMLDecodeError 会被引发,在无效 TOML (汤姆的明显最小语言) 文档。

下列异常是可用的:

exception tomllib. TOMLDecodeError

子类化的 ValueError .

范例

剖析 TOML (汤姆的明显最小语言) 文件:

import tomllib
with open("pyproject.toml", "rb") as f:
    data = tomllib.load(f)