tomllib — Parse TOML files — Python 3.15.2 documentation
Навігація індекс модулі | наступний | попередній | Python » 3.15.2 documentation » The Python standard library » Формати файлів » tomllib — Parse TOML files | Theme | tomllib — Parse TOML files ¶ Source code: Lib/tomllib This module provides an interface for parsing TOML 1.1.0 (Tom’s Obvious Minimal Language, https://toml.io ). This module does not support writing TOML.
Added in version 3.11: The module was added with support for TOML 1.0.0. Змінено в версії 3.15: Added TOML 1.1.0 support. See the What’s New for details. Попередження 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. Дивись також 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 modules.
Дивись також 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 ) ¶ Read a TOML file. The first argument should be a readable and binary file object. Return a dict . Convert TOML types to Python using this conversion table . 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 a dict or a list , else a ValueError is raised. A TOMLDecodeError will be raised on an invalid TOML document. tomllib. loads ( s , / , * , parse_float = float ) ¶ Load TOML from a str object. Return a dict . Convert TOML types to Python using this conversion table .
The parse_float argument has the same meaning as in load() . A TOMLDecodeError will be raised on an invalid TOML document. The following exceptions are available: exception tomllib. TOMLDecodeError ( msg , doc , pos ) ¶ Subclass of ValueError 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 .
Змінено в версії 3.14: Added the msg , doc and pos parameters. Added the msg , doc , pos , lineno and colno attributes. Застаріло починаючи з версії 3.14: Passing free-form positional arguments is deprecated. Приклади ¶ Parsing a TOML file: 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 ) Conversion Table ¶ TOML Python TOML document дикт рядок вул ціле число int плавати float (configurable with parse_float ) логічний 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 масив список table дикт inline table дикт array of tables list of dicts Limits and interoperability considerations ¶ tomllib places some limits on the documents it can handle, and it preserves details that other TOML parsers are allowed to ignore.
When writing portable TOML files, only use features that are guaranteed or recommended by the standard. The implementation details listed here may change in future versions of Python. Tables/dicts The TOML spec does not guarantee key/value pairs in TOML documents and tables to be in any specific order. Деталі реалізації CPython: tomllib loads dictionary entries in the order they appear in the source. Integers TOML recommends supporting integers in range(-2**63, 2**63) .
Деталі реалізації CPython: tomllib uses Python’s limit on integer string conversion (4300 digits by default). Floats TOML recommends supporting at least IEEE 754 binary64 values, which means that numbers with more than 15 significant decimal digits are likely to be rounded. Деталі реалізації CPython: tomllib uses Python float by default; on many common platforms this is the recommended binary64.
See sys.float_info for details. Nesting limit TOML 1.1.0 does not recommend a limit on how deeply arrays and tables may be nested inside one another. (A limit of 100 has been proposed for a future version of TOML.) Деталі реалізації CPython: In tomllib , the nesting level is mainly limited by Python’s recursion limit . Note that code that calls tomllib may contribute to the limit. Table of Contents tomllib — Parse TOML files Приклади Conversion Table Limits and interoperability considerations Попередній розділ configparser — Configuration file parser Наступна тема netrc — netrc file processing This page Report a bug Improve this page Show source Show translation source « Навігація індекс модулі | наступний | попередній | Python » 3.15.2 documentation » The Python standard library » Формати файлів » tomllib — Parse TOML files | Theme | © Авторське право 2001 Python Software Foundation.
This page is licensed under the Python Software Foundation License Version 2. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License. See History and License for more information. The Python Software Foundation is a non-profit corporation. Please donate. Востаннє оновлено лист. 19, 2026 (11:14 UTC). Found a bug ? Created using Sphinx 9.1.1.