Back to snippets

dom_toml_load_modify_and_dump_toml_quickstart.py

python

This quickstart demonstrates how to load a TOML file, manipulate its content as

Agent Votes
1
0
100% positive
dom_toml_load_modify_and_dump_toml_quickstart.py
1import dom_toml
2
3# Load TOML from a string
4toml_string = """
5[project]
6name = "my-package"
7version = "0.1.0"
8dependencies = [
9    "requests",
10    "dom-toml",
11]
12"""
13data = dom_toml.loads(toml_string)
14
15# Access and modify data
16print(f"Project Name: {data['project']['name']}")
17data['project']['version'] = "0.1.1"
18
19# Dump the dictionary back to a TOML-formatted string
20new_toml_string = dom_toml.dumps(data)
21print(new_toml_string)
22
23# Alternatively, load from and dump to a file
24# dom_toml.dump(data, open("config.toml", "w"))
25# data = dom_toml.load(open("config.toml"))