A lil' TOML writer (counterpart to https://github.com/hukkin/tomli)
Python
146
95 commits
updated Oct 5, 2026
A lil' TOML writer
Table of Contents generated with mdformat-toc
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.
pip install tomli-w
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
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)
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.
No.
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
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
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:
__str__ method.Given proper input (a mapping consisting of non-subclassed types returned by Tomli) the output should be valid TOML.
107 followers · starred Aug 2022
104 followers · starred Aug 2021
404 followers · starred Jul 2021
114 followers · starred Dec 2023
A lil' TOML writer (counterpart to https://github.com/hukkin/tomli)
Python
146
95 commits
updated Oct 5, 2026
A lil' TOML writer
Table of Contents generated with mdformat-toc
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.
pip install tomli-w
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
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)
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.
No.
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
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
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:
__str__ method.Given proper input (a mapping consisting of non-subclassed types returned by Tomli) the output should be valid TOML.
107 followers · starred Aug 2022
104 followers · starred Aug 2021
404 followers · starred Jul 2021
114 followers · starred Dec 2023