14.1. TOML About
14.1.1. SetUp
>>> import tomllib
14.1.2. From String
>>> data = """
... project = "myproject"
... version = "1.0.0"
... """
>>>
>>> data = tomllib.loads(data)
>>> data
{'project': 'myproject', 'version': '1.0.0'}
14.1.3. From File
project = "myproject"
version = "1.0.0"
>>> with open('/tmp/myfile.toml', mode='rb') as file:
... data = tomllib.load(file)
14.1.4. Conversion Table
TOML's table` is Python's
dict
TOML's string` is Python's
str
TOML's integer` is Python's
int
TOML's float` is Python's
float
TOML's boolean` is Python's
bool
TOML's offset datetime` is Python's
datetime.datetime
(tzinfo
attribute set to an instance ofdatetime.timezone
)TOML's local datetime` is Python's
datetime.datetime
(tzinfo
attribute set toNone
)TOML's local date` is Python's
datetime.date
TOML's local time` is Python's
datetime.time
TOML's array` is Python's
list
14.1.5. Example
[project]
name = "myproject"
version = "1.0.0"
requires-python = ">=3.13"
authors = [{name = "Alice", email = "alice@example.com"}]
readme = "README.md"
license = {file = "LICENSE"}
keywords = ["myproject", "myapp", "python", "django", "ninja"]
dependencies = [
"django == 5.2.*",
"django-ninja == 1.4.*"]