pytoml

0.1.21 · deprecated · verified Tue Apr 14

pytoml is a Python library designed for parsing and writing TOML (Tom's Obvious, Minimal Language) files. It specifically targets version 0.4.0 of the TOML specification. The library provides an interface similar to Python's standard `json` module, offering `load`, `loads`, `dump`, and `dumps` functions. The project is no longer actively maintained, with its last release (0.1.21) in July 2019.

Warnings

Install

Imports

Quickstart

Demonstrates loading TOML data from a string and a file-like object, and dumping a Python dictionary to a TOML string. Note that `load` expects a binary-read file object (e.g., opened with 'rb').

import pytoml as toml
import io

# Loading from a string
toml_string = 'title = "My TOML Document"\nowner = { name = "John Doe" }'
data_from_string = toml.loads(toml_string)
print(f"From string: {data_from_string}")

# Loading from a file-like object (e.g., a file)
# Using io.BytesIO to simulate reading a file in binary mode ('rb')
toml_file_content = b'[database]\nserver = "192.168.1.1"\nports = [8001, 8002, 8003]'
file_like_object = io.BytesIO(toml_file_content)
data_from_file = toml.load(file_like_object)
print(f"From file-like object: {data_from_file}")

# Dumping to a string
dict_to_dump = {'server': '10.0.0.1', 'connection_max': 5000}
dumped_string = toml.dumps(dict_to_dump)
print(f"Dumped TOML string:\n{dumped_string}")

view raw JSON →