| single |
[Build Status]
[![codecov.io]](https://codecov.io/gh/hukkin/tomli-w)
[PyPI version]
# Tomli-W
> A lil' TOML writer
**Table of Contents** *generated with [mdformat-toc]*
- [Intro]
- [Installation]
- [Usage]
- [Write to string]
- [Write to file]
- [FAQ]
- [Does Tomli-W sort the document?]
- [Does Tomli-W support writing documents with comments?]
- [Can I customize insignificant whitespace?]
- [Why does Tomli-W not write a multi-line string if the string value
contains newlines?]
- [Is Tomli-W output guaranteed to be valid TOML?]
## 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
```bash
pip install tomli-w
```
## Usage
### Write to string
```python
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
```python
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.
```python
import tomli_w
doc = {"fruits": ["orange", "kiwi", "papaya"]}
expected_toml = """\
fruits = [
"orange",
"kiwi",
"papaya",
]
"""
|