内容表

  • tomllib — 剖析 TOML 文件
    • 范例
    • 转换表

上一话题

configparser — 配置文件剖析器

下一话题
就业培训     下载中心     Wiki     联络
登录   注册

Log
  1. 首页
  2. Python 3.12.4
  3. 索引
  4. 模块
  5. 下一
  6. 上一
  7. Python 标准库
  8. 文件格式
  9. tomllib — 剖析 TOML 文件

tomllib — 剖析 TOML 文件 ¶

Added in version 3.11.

源代码: Lib/tomllib


本模块提供的接口用于剖析 TOML (汤姆的明显最小语言 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)
										

Parsing a TOML string:

import tomllib
toml_str = """
python-version = "3.11.0"
python-implementation = "CPython"
"""
data = tomllib.loads(toml_str)
										

转换表 ¶

TOML

Python

TOML document 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
table dict
inline table dict
array of tables list of dicts

内容表

  • tomllib — 剖析 TOML 文件
    • 范例
    • 转换表

上一话题

configparser — 配置文件剖析器

下一话题

netrc — netrc 文件处理

本页

  • 报告 Bug
  • 展示源

快速搜索

键入搜索术语或模块、类、函数名称。

  1. 首页
  2. Python 3.12.4
  3. 索引
  4. 模块
  5. 下一
  6. 上一
  7. Python 标准库
  8. 文件格式
  9. tomllib — 剖析 TOML 文件

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1