diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/python~3.12/library%2Ftomllib.html | |
new repository
Diffstat (limited to 'devdocs/python~3.12/library%2Ftomllib.html')
| -rw-r--r-- | devdocs/python~3.12/library%2Ftomllib.html | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/devdocs/python~3.12/library%2Ftomllib.html b/devdocs/python~3.12/library%2Ftomllib.html new file mode 100644 index 00000000..ef1e1f68 --- /dev/null +++ b/devdocs/python~3.12/library%2Ftomllib.html @@ -0,0 +1,41 @@ + <span id="tomllib-parse-toml-files"></span><h1>tomllib — Parse TOML files</h1> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.11.</span></p> </div> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/tomllib">Lib/tomllib</a></p> <p>This module provides an interface for parsing TOML (Tom’s Obvious Minimal Language, <a class="reference external" href="https://toml.io/en/">https://toml.io</a>). This module does not support writing TOML.</p> <div class="admonition seealso"> <p class="admonition-title">See also</p> <p>The <a class="reference external" href="https://pypi.org/project/tomli-w/">Tomli-W package</a> is a TOML writer that can be used in conjunction with this module, providing a write API familiar to users of the standard library <a class="reference internal" href="marshal#module-marshal" title="marshal: Convert Python objects to streams of bytes and back (with different constraints)."><code>marshal</code></a> and <a class="reference internal" href="pickle#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><code>pickle</code></a> modules.</p> </div> <div class="admonition seealso"> <p class="admonition-title">See also</p> <p>The <a class="reference external" href="https://pypi.org/project/tomlkit/">TOML Kit package</a> 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.</p> </div> <p>This module defines the following functions:</p> <dl class="py function"> <dt class="sig sig-object py" id="tomllib.load"> +<code>tomllib.load(fp, /, *, parse_float=float)</code> </dt> <dd> +<p>Read a TOML file. The first argument should be a readable and binary file object. Return a <a class="reference internal" href="stdtypes#dict" title="dict"><code>dict</code></a>. Convert TOML types to Python using this <a class="reference internal" href="#toml-to-py-table"><span class="std std-ref">conversion table</span></a>.</p> <p><em>parse_float</em> will be called with the string of every TOML float to be decoded. By default, this is equivalent to <code>float(num_str)</code>. This can be used to use another datatype or parser for TOML floats (e.g. <a class="reference internal" href="decimal#decimal.Decimal" title="decimal.Decimal"><code>decimal.Decimal</code></a>). The callable must not return a <a class="reference internal" href="stdtypes#dict" title="dict"><code>dict</code></a> or a <a class="reference internal" href="stdtypes#list" title="list"><code>list</code></a>, else a <a class="reference internal" href="exceptions#ValueError" title="ValueError"><code>ValueError</code></a> is raised.</p> <p>A <a class="reference internal" href="#tomllib.TOMLDecodeError" title="tomllib.TOMLDecodeError"><code>TOMLDecodeError</code></a> will be raised on an invalid TOML document.</p> </dd> +</dl> <dl class="py function"> <dt class="sig sig-object py" id="tomllib.loads"> +<code>tomllib.loads(s, /, *, parse_float=float)</code> </dt> <dd> +<p>Load TOML from a <a class="reference internal" href="stdtypes#str" title="str"><code>str</code></a> object. Return a <a class="reference internal" href="stdtypes#dict" title="dict"><code>dict</code></a>. Convert TOML types to Python using this <a class="reference internal" href="#toml-to-py-table"><span class="std std-ref">conversion table</span></a>. The <em>parse_float</em> argument has the same meaning as in <a class="reference internal" href="#tomllib.load" title="tomllib.load"><code>load()</code></a>.</p> <p>A <a class="reference internal" href="#tomllib.TOMLDecodeError" title="tomllib.TOMLDecodeError"><code>TOMLDecodeError</code></a> will be raised on an invalid TOML document.</p> </dd> +</dl> <p>The following exceptions are available:</p> <dl class="py exception"> <dt class="sig sig-object py" id="tomllib.TOMLDecodeError"> +<code>exception tomllib.TOMLDecodeError</code> </dt> <dd> +<p>Subclass of <a class="reference internal" href="exceptions#ValueError" title="ValueError"><code>ValueError</code></a>.</p> </dd> +</dl> <section id="examples"> <h2>Examples</h2> <p>Parsing a TOML file:</p> <pre data-language="python">import tomllib + +with open("pyproject.toml", "rb") as f: + data = tomllib.load(f) +</pre> <p>Parsing a TOML string:</p> <pre data-language="python">import tomllib + +toml_str = """ +python-version = "3.11.0" +python-implementation = "CPython" +""" + +data = tomllib.loads(toml_str) +</pre> </section> <section id="conversion-table"> <h2>Conversion Table</h2> <table class="docutils align-default" id="toml-to-py-table"> <thead> <tr> +<th class="head"><p>TOML</p></th> <th class="head"><p>Python</p></th> </tr> </thead> <tr> +<td><p>TOML document</p></td> <td><p>dict</p></td> </tr> <tr> +<td><p>string</p></td> <td><p>str</p></td> </tr> <tr> +<td><p>integer</p></td> <td><p>int</p></td> </tr> <tr> +<td><p>float</p></td> <td><p>float (configurable with <em>parse_float</em>)</p></td> </tr> <tr> +<td><p>boolean</p></td> <td><p>bool</p></td> </tr> <tr> +<td><p>offset date-time</p></td> <td><p>datetime.datetime (<code>tzinfo</code> attribute set to an instance of <code>datetime.timezone</code>)</p></td> </tr> <tr> +<td><p>local date-time</p></td> <td><p>datetime.datetime (<code>tzinfo</code> attribute set to <code>None</code>)</p></td> </tr> <tr> +<td><p>local date</p></td> <td><p>datetime.date</p></td> </tr> <tr> +<td><p>local time</p></td> <td><p>datetime.time</p></td> </tr> <tr> +<td><p>array</p></td> <td><p>list</p></td> </tr> <tr> +<td><p>table</p></td> <td><p>dict</p></td> </tr> <tr> +<td><p>inline table</p></td> <td><p>dict</p></td> </tr> <tr> +<td><p>array of tables</p></td> <td><p>list of dicts</p></td> </tr> </table> </section> <div class="_attribution"> + <p class="_attribution-p"> + © 2001–2023 Python Software Foundation<br>Licensed under the PSF License.<br> + <a href="https://docs.python.org/3.12/library/tomllib.html" class="_attribution-link">https://docs.python.org/3.12/library/tomllib.html</a> + </p> +</div> |
