tomllib — Analizar archivos TOML¶
Nuevo en la versión 3.11.
Código fuente: Lib/tomllib
This module provides an interface for parsing TOML (Tom’s Obvious Minimal Language, https://toml.io). This module does not support writing TOML.
Ver también
El paquete Tomli-W es un escritor de TOML que puede utilizarse junto con este módulo, proporcionando una API de escritura familiar para los usuarios de los módulos marshal y pickle de la biblioteca estándar.
Ver también
El paquete TOML Kit es una biblioteca TOML que preserva el estilo y tiene capacidad de lectura y escritura. Se recomienda sustituir este módulo para editar archivos TOML ya existentes.
Este módulo define las siguientes funciones:
- tomllib.load(fp, /, *, parse_float=float)¶
Lee un archivo TOML. El primer argumento debe ser un objeto de archivo legible y binario. Retorna un
dict. Convierte los tipos TOML a Python utilizando esta tabla de conversión.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). The callable must not return adictor alist, else aValueErroris raised.Un
TOMLDecodeErrorse levantará en un documento TOML inválido.
- tomllib.loads(s, /, *, parse_float=float)¶
Carga TOML de un objeto
str. Retorna undict. Convierte los tipos TOML a Python utilizando esta tabla de conversión. El argumento parse_float tiene el mismo significado que enload().Un
TOMLDecodeErrorse levantará en un documento TOML inválido.
Las siguientes excepciones están disponibles:
- exception tomllib.TOMLDecodeError¶
Subclase de
ValueError.
Ejemplos¶
Analiza un TOML file:
import tomllib
with open("pyproject.toml", "rb") as f:
data = tomllib.load(f)
Analiza un TOML string:
import tomllib
toml_str = """
python-version = "3.11.0"
python-implementation = "CPython"
"""
data = tomllib.loads(toml_str)
Tabla de conversión¶
TOML |
Python |
|---|---|
TOML document |
dict |
cadena |
str |
integer |
int |
flotante |
flotante (configurable con parse_float) |
boolean |
bool |
offset date-time |
datetime.datetime (atributo |
local date-time |
datetime.datetime (atributo |
local date |
datetime.date |
local time |
datetime.time |
array |
list |
tabla |
dict |
inline table |
dict |
array of tables |
list of dicts |