tomledit

1.1.0 · active · verified Fri Apr 17

tomledit is a Python library designed for parsing and editing TOML files while meticulously preserving comments, whitespace, and the original order of elements. It is especially useful for managing configuration files where maintaining human-readable formatting is critical. The current version is 1.1.0, and it is actively maintained with releases made as needed.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to parse a TOML string, modify existing values, add new key-value pairs, and print the resulting document, all while preserving the original comments and formatting.

from tomledit import TomlDocument

toml_content = """
# Main config
[server]
host = "localhost" # Server IP
port = 8080

[database]
type = "PostgreSQL"
user = "admin"
"""

# Parse the TOML content
doc = TomlDocument.parse(toml_content)

print("--- Original Document ---")
print(doc)

# Modify values, preserving comments and format
doc["server"]["host"] = "127.0.0.1"
doc["database"]["user"] = "dbuser"

# Add a new key to an existing table
doc["database"]["password"] = "securepassword"

print("\n--- Modified Document ---")
print(doc)

view raw JSON →