Skip to main content

Build Status codecov.io PyPI version

Tomli-W

A lil' TOML writer

Table of Contents generated with mdformat-toc

Intro

Tomli-W is a Python library for writing TOML. It is a write-only counterpart to Tomli, which is a read-only TOML parser. Tomli-W is fully compatible with TOML v1.0.0.

Installation

pip install tomli-w

Usage

Write to string

import tomli_w

doc = {"table": {"nested": {}, "val3": 3}, "val2": 2, "val1": 1}
expected_toml = """\
val2 = 2
val1 = 1

[table]
val3 = 3

[table.nested]
"""
assert tomli_w.dumps(doc) == expected_toml

Write to file

import tomli_w

doc = {"one": 1, "two": 2, "pi": 3}
with open("path_to_file/conf.toml", "wb") as f:
    tomli_w.dump(doc, f)

FAQ

Does Tomli-W sort the document?

No, but it respects sort order of the input data, so one could sort the content of the dict (recursively) before calling tomli_w.dumps.

Does Tomli-W support writing documents with comments?

No.

Can I customize insignificant whitespace?

Indent width of array content can be configured via the indent keyword argument. indent takes a non-negative integer, defaulting to 4.

import tomli_w

doc = {"fruits": ["orange", "kiwi", "papaya"]}
expected_toml = """\
fruits = [
 "orange",
 "kiwi",
 "papaya",
]
"""
assert tomli_w.dumps(doc, indent=1) == expected_toml

Why does Tomli-W not write a multi-line string if the string value contains newlines?

This default was chosen to achieve lossless parse/write round-trips.

TOML strings can contain newlines where exact bytes matter, e.g.

s = "here's a newline\r\n"

TOML strings also can contain newlines where exact byte representation is not relevant, e.g.

s = """here's a newline
"""

A parse/write round-trip that converts the former example to the latter does not preserve the original newline byte sequence. This is why Tomli-W avoids writing multi-line strings.

A keyword argument is provided for users who do not need newline bytes to be preserved:

import tomli_w

doc = {"s": "here's a newline\r\n"}
expected_toml = '''\
s = """
here's a newline
"""
'''
assert tomli_w.dumps(doc, multiline_strings=True) == expected_toml

Is Tomli-W output guaranteed to be valid TOML?

No. If there's a chance that your input data is bad and you need output validation, parse the output string once with tomli.loads. If the parse is successful (does not raise tomli.TOMLDecodeError) then the string is valid TOML.

Examples of bad input data that can lead to writing invalid TOML without an error being raised include:

  • A mapping where keys behave very much like strings, but aren't. E.g. a tuple of strings of length 1.
  • A mapping where a value is a subclass of a supported type, but which overrides the __str__ method.

Given proper input (a mapping consisting of non-subclassed types returned by Tomli) the output should be valid TOML.

Metadata

Release files for tomli-w 1.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tomli-w 1.2.0
File Size Uploaded
tomli_w-1.2.0.tar.gz 7.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tomli-w 1.2.0
File Interpreter ABI Platform
tomli_w-1.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 13.9 kB

Release files / tomli_w-1.2.0.tar.gz

Download URL tomli_w-1.2.0.tar.gz
Size 7.2 kB
Tags Source
SHA-256 checksum
How to use checksums
2dd14fac5a47c27be9cd4c976af5a12d87fb1f0b4512f81d69cce3b35ae25021
BLAKE2b-256 checksum
How to use checksums
1975241269d1da26b624c0d5e110e8149093c759b7a286138f4efd61a60e75fe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.8

Release files / tomli_w-1.2.0-py3-none-any.whl

Download URL tomli_w-1.2.0-py3-none-any.whl
Size 6.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
188306098d013b691fcadc011abd66727d3c414c571bb01b1a174ba8c983cf90
BLAKE2b-256 checksum
How to use checksums
c718c86eb8e0202e32dd3df50d43d7ff9854f8e0603945ff398974c1d91ac1ef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.8

Release history Release notifications | RSS feed

This release

1.2.0 This release

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page