TOML
A Python library for parsing and creating TOML (Tom's Obvious, Minimal Language) files. Current version: 0.10.2. Maintained with regular updates.
Warnings
- breaking In version 0.10.0, the 'load' function was removed. Use 'loads' for strings and 'load' for file objects.
- gotcha Ensure 'toml' is imported before using its functions like 'loads', 'dumps', and 'dump'.
Install
-
pip install toml
Imports
- loads
import toml parsed_toml = toml.loads(toml_string)
- dumps
import toml new_toml_string = toml.dumps(parsed_toml)
- dump
import toml with open('new_toml_file.toml', 'w') as f: toml.dump(parsed_toml, f)
Quickstart
import toml toml_string = ''' # This is a TOML document. title = "TOML Example" [owner] name = "Tom Preston-Werner" dob = 1979-05-27T07:32:00-08:00 # First class dates [database] server = "192.168.1.1" ports = [ 8001, 8001, 8002 ] connection_max = 5000 enabled = true [servers] # Indentation (tabs and/or spaces) is allowed but not required [servers.alpha] ip = "10.0.0.1" dc = "eqdc10" [servers.beta] ip = "10.0.0.2" dc = "eqdc10" [clients] data = [ ["gamma", "delta"], [1, 2] ] # Line breaks are OK when inside arrays hosts = [ "alpha", "omega" ] ''' parsed_toml = toml.loads(toml_string) new_toml_string = toml.dumps(parsed_toml) print(new_toml_string)