tomllib
— 剖析 TOML 文件
¶
3.11 版新增。
源代码: Lib/tomllib
本模块提供的接口用于剖析 TOML (汤姆的明显最小语言 https://toml.io )。本模块不支持写入 TOML。
另请参阅
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
模块。
另请参阅
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.
此模块定义了下列函数:
读取 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 (汤姆的明显最小语言) 文档。
加载 TOML 从
str
对象。返回
dict
。把 TOML 类型转换到 Python 使用此
转换表
。
parse_float
自变量拥有相同含义如在
load()
.
A
TOMLDecodeError
会被引发,在无效 TOML (汤姆的明显最小语言) 文档。
下列异常是可用的:
子类化的
ValueError
.
剖析 TOML (汤姆的明显最小语言) 文件:
import tomllib with open("pyproject.toml", "rb") as f: data = tomllib.load(f)
Parsing a TOML string:
import tomllib toml_str = """ python-version = "3.11.0" python-implementation = "CPython" """ data = tomllib.loads(toml_str)
| TOML | Python |
|---|---|
| table | dict |
| string | str |
| integer | int |
| float | float (configurable with parse_float ) |
| boolean | bool |
| offset date-time |
datetime.datetime (
tzinfo
attribute set to an instance of
datetime.timezone
)
|
| local date-time |
datetime.datetime (
tzinfo
属性设置为
None
)
|
| local date | datetime.date |
| local time | datetime.time |
| array | list |