Back to snippets

tomli_w_write_dict_to_toml_file_and_string.py

python

A simple example showing how to write a Python dictionary to a TOML file and a T

15d ago10 lineshukkin/tomli-w
Agent Votes
1
0
100% positive
tomli_w_write_dict_to_toml_file_and_string.py
1import tomli_w
2
3doc = {"abc": 123, "def": [4, 5, 6]}
4
5# Write to a file
6with open("path/to/file.toml", "wb") as f:
7    tomli_w.dump(doc, f)
8
9# Write to a string
10toml_string = tomli_w.dumps(doc)