Added in version 3.11.
Source code:
———
This module provides an interface for parsing TOML 1.0.0 (Tom’s Obvious Minimal Language,
). This module does not support writing TOML.
Warning
Be cautious when parsing data from untrusted sources. A malicious TOML string may cause the decoder to consume considerable CPU and memory resources. Limiting the size of data to be parsed is recommended.
See also
The
is a TOML writer that can be used in conjunction with this module, providing a write API familiar to users of the standard library
and
modules.
See also
The
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.
This module defines the following functions:
tomllib.load(fp, /, *, parse_float=float)
Read a TOML file. The first argument should be a readable and binary file object. Return a
. Convert TOML types to Python using this
.
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.
). The callable must not return a
or a
, else a
is raised.
A
will be raised on an invalid TOML document.
tomllib.loads(s, /, *, parse_float=float)
Load TOML from a
object. Return a
. Convert TOML types to Python using this
. The parse_float argument has the same meaning as in
.
A
will be raised on an invalid TOML document.
The following exceptions are available:
exceptiontomllib.TOMLDecodeError(msg, doc, pos)
Subclass of
with the following additional attributes:
msg
The unformatted error message.
doc
The TOML document being parsed.
pos
The index of doc where parsing failed.
lineno
The line corresponding to pos.
colno
The column corresponding to pos.
Changed in version 3.14: Added the msg, doc and pos parameters. Added the
,
,
,
and
attributes.
Deprecated since version 3.14: Passing free-form positional arguments is deprecated.
Examples
Parsing a TOML file:
importtomllibwithopen("pyproject.toml","rb")asf:data=tomllib.load(f)Parsing a TOML string:
importtomllibtoml_str="""python-version = "3.11.0"python-implementation = "CPython""""data=tomllib.loads(toml_str)Conversion Table
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 attribute set to None)
local date
datetime.date
local time
datetime.time
array
list
table
dict
inline table
dict
array of tables
list of dicts