TOML

raw JSON →
0.10.2 verified Tue May 12 auth: no python install: verified quickstart: verified

A Python library for parsing and creating TOML (Tom's Obvious, Minimal Language) files. Current version: 0.10.2. Maintained with regular updates.

pip install toml
error ModuleNotFoundError: No module named 'toml'
cause The 'toml' package is not installed in the current Python environment.
fix
Run pip install toml in your terminal to install the library.
error toml.decoder.TomlParsingError: Unexpected character at line X column Y
cause The TOML file contains a syntax error at the specified line and column, such as an invalid character, missing quotes, or incorrect structure.
fix
Review and correct the TOML file syntax according to the TOML specification, paying close attention to the indicated line and column.
error toml.decoder.TomlParsingError: Duplicate key 'keyname'
cause A TOML file contains the same key defined multiple times within the same table, which is not allowed by the TOML specification.
fix
Ensure each key within a TOML table is unique; remove or rename duplicate keys to comply with the TOML standard.
error TypeError: Value for key 'x' of type <class 'set'> is not a TOML-supported type.
cause The `toml.dump()` function was called with a Python object containing a type (e.g., set) that does not have a direct equivalent in the TOML specification.
fix
Convert the unsupported Python object type to a TOML-compatible type (e.g., list, string, integer, float, boolean, datetime) before attempting to dump it to a TOML file.
breaking In version 0.10.0, the 'load' function was removed. Use 'loads' for strings and 'load' for file objects.
fix Replace 'load' with 'loads' for strings and 'load' for file objects.
gotcha Ensure 'toml' is imported before using its functions like 'loads', 'dumps', and 'dump'.
fix Always import 'toml' at the beginning of your script.
python os / libc status wheel install import disk
3.10 alpine (musl) - - 0.01s 17.9M
3.10 slim (glibc) - - 0.01s 18M
3.11 alpine (musl) - - 0.01s 19.8M
3.11 slim (glibc) - - 0.01s 20M
3.12 alpine (musl) - - 0.01s 11.6M
3.12 slim (glibc) - - 0.01s 12M
3.13 alpine (musl) - - 0.01s 11.3M
3.13 slim (glibc) - - 0.01s 12M
3.9 alpine (musl) - - 0.01s 17.4M
3.9 slim (glibc) - - 0.01s 18M

A comprehensive example demonstrating parsing and generating TOML data using the 'toml' library.

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)