{"id":16728788,"url":"https://github.com/hukkin/tomli-w","last_synced_at":"2025-05-15T17:03:15.500Z","repository":{"id":39620462,"uuid":"381473785","full_name":"hukkin/tomli-w","owner":"hukkin","description":"A lil' TOML writer (counterpart to https://github.com/hukkin/tomli)","archived":false,"fork":false,"pushed_at":"2025-03-31T17:18:18.000Z","size":115,"stargazers_count":104,"open_issues_count":3,"forks_count":16,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-05-15T00:21:29.191Z","etag":null,"topics":["config","python","toml","tomli"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hukkin.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-06-29T19:20:20.000Z","updated_at":"2025-05-07T14:12:27.000Z","dependencies_parsed_at":"2023-12-11T18:44:27.150Z","dependency_job_id":"379b8ddc-b2ee-4211-9e55-32a988c8780a","html_url":"https://github.com/hukkin/tomli-w","commit_stats":{"total_commits":75,"total_committers":5,"mean_commits":15.0,"dds":0.12,"last_synced_commit":"ce2a46649c0f4323f35f91bc3254056269360d42"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hukkin%2Ftomli-w","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hukkin%2Ftomli-w/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hukkin%2Ftomli-w/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hukkin%2Ftomli-w/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hukkin","download_url":"https://codeload.github.com/hukkin/tomli-w/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254384937,"owners_count":22062421,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["config","python","toml","tomli"],"created_at":"2024-10-12T23:11:44.012Z","updated_at":"2025-05-15T17:03:15.480Z","avatar_url":"https://github.com/hukkin.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://github.com/hukkin/tomli-w/actions/workflows/tests.yaml/badge.svg?branch=master)](https://github.com/hukkin/tomli-w/actions?query=workflow%3ATests+branch%3Amaster+event%3Apush)\n[![codecov.io](https://codecov.io/gh/hukkin/tomli-w/branch/master/graph/badge.svg)](https://codecov.io/gh/hukkin/tomli-w)\n[![PyPI version](https://img.shields.io/pypi/v/tomli-w)](https://pypi.org/project/tomli-w)\n\n# Tomli-W\n\n\u003e A lil' TOML writer\n\n**Table of Contents** *generated with [mdformat-toc](https://github.com/hukkin/mdformat-toc)*\n\n\u003c!-- mdformat-toc start --slug=github --maxlevel=6 --minlevel=2 --\u003e\n\n- [Intro](#intro)\n- [Installation](#installation)\n- [Usage](#usage)\n  - [Write to string](#write-to-string)\n  - [Write to file](#write-to-file)\n- [FAQ](#faq)\n  - [Does Tomli-W sort the document?](#does-tomli-w-sort-the-document)\n  - [Does Tomli-W support writing documents with comments?](#does-tomli-w-support-writing-documents-with-comments)\n  - [Can I customize insignificant whitespace?](#can-i-customize-insignificant-whitespace)\n  - [Why does Tomli-W not write a multi-line string if the string value contains newlines?](#why-does-tomli-w-not-write-a-multi-line-string-if-the-string-value-contains-newlines)\n  - [Is Tomli-W output guaranteed to be valid TOML?](#is-tomli-w-output-guaranteed-to-be-valid-toml)\n\n\u003c!-- mdformat-toc end --\u003e\n\n## Intro\u003ca name=\"intro\"\u003e\u003c/a\u003e\n\nTomli-W is a Python library for writing [TOML](https://toml.io).\nIt is a write-only counterpart to [Tomli](https://github.com/hukkin/tomli),\nwhich is a read-only TOML parser.\nTomli-W is fully compatible with [TOML v1.0.0](https://toml.io/en/v1.0.0).\n\n## Installation\u003ca name=\"installation\"\u003e\u003c/a\u003e\n\n```bash\npip install tomli-w\n```\n\n## Usage\u003ca name=\"usage\"\u003e\u003c/a\u003e\n\n### Write to string\u003ca name=\"write-to-string\"\u003e\u003c/a\u003e\n\n```python\nimport tomli_w\n\ndoc = {\"table\": {\"nested\": {}, \"val3\": 3}, \"val2\": 2, \"val1\": 1}\nexpected_toml = \"\"\"\\\nval2 = 2\nval1 = 1\n\n[table]\nval3 = 3\n\n[table.nested]\n\"\"\"\nassert tomli_w.dumps(doc) == expected_toml\n```\n\n### Write to file\u003ca name=\"write-to-file\"\u003e\u003c/a\u003e\n\n```python\nimport tomli_w\n\ndoc = {\"one\": 1, \"two\": 2, \"pi\": 3}\nwith open(\"path_to_file/conf.toml\", \"wb\") as f:\n    tomli_w.dump(doc, f)\n```\n\n## FAQ\u003ca name=\"faq\"\u003e\u003c/a\u003e\n\n### Does Tomli-W sort the document?\u003ca name=\"does-tomli-w-sort-the-document\"\u003e\u003c/a\u003e\n\nNo, but it respects sort order of the input data,\nso one could sort the content of the `dict` (recursively) before calling `tomli_w.dumps`.\n\n### Does Tomli-W support writing documents with comments?\u003ca name=\"does-tomli-w-support-writing-documents-with-comments\"\u003e\u003c/a\u003e\n\nNo.\n\n### Can I customize insignificant whitespace?\u003ca name=\"can-i-customize-insignificant-whitespace\"\u003e\u003c/a\u003e\n\nIndent width of array content can be configured via the `indent` keyword argument.\n`indent` takes a non-negative integer, defaulting to 4.\n\n```python\nimport tomli_w\n\ndoc = {\"fruits\": [\"orange\", \"kiwi\", \"papaya\"]}\nexpected_toml = \"\"\"\\\nfruits = [\n \"orange\",\n \"kiwi\",\n \"papaya\",\n]\n\"\"\"\nassert tomli_w.dumps(doc, indent=1) == expected_toml\n```\n\n### Why does Tomli-W not write a multi-line string if the string value contains newlines?\u003ca name=\"why-does-tomli-w-not-write-a-multi-line-string-if-the-string-value-contains-newlines\"\u003e\u003c/a\u003e\n\nThis default was chosen to achieve lossless parse/write round-trips.\n\nTOML strings can contain newlines where exact bytes matter, e.g.\n\n```toml\ns = \"here's a newline\\r\\n\"\n```\n\nTOML strings also can contain newlines where exact byte representation is not relevant, e.g.\n\n```toml\ns = \"\"\"here's a newline\n\"\"\"\n```\n\nA parse/write round-trip that converts the former example to the latter does not preserve the original newline byte sequence.\nThis is why Tomli-W avoids writing multi-line strings.\n\nA keyword argument is provided for users who do not need newline bytes to be preserved:\n\n```python\nimport tomli_w\n\ndoc = {\"s\": \"here's a newline\\r\\n\"}\nexpected_toml = '''\\\ns = \"\"\"\nhere's a newline\n\"\"\"\n'''\nassert tomli_w.dumps(doc, multiline_strings=True) == expected_toml\n```\n\n### Is Tomli-W output guaranteed to be valid TOML?\u003ca name=\"is-tomli-w-output-guaranteed-to-be-valid-toml\"\u003e\u003c/a\u003e\n\nNo.\nIf there's a chance that your input data is bad and you need output validation,\nparse the output string once with `tomli.loads`.\nIf the parse is successful (does not raise `tomli.TOMLDecodeError`) then the string is valid TOML.\n\nExamples of bad input data that can lead to writing invalid TOML without an error being raised include:\n\n- A mapping where keys behave very much like strings, but aren't. E.g. a tuple of strings of length 1.\n- A mapping where a value is a subclass of a supported type, but which overrides the `__str__` method.\n\nGiven proper input (a mapping consisting of non-subclassed [types returned by Tomli](https://github.com/hukkin/tomli?tab=readme-ov-file#how-do-toml-types-map-into-python-types))\nthe output should be valid TOML.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhukkin%2Ftomli-w","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhukkin%2Ftomli-w","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhukkin%2Ftomli-w/lists"}