13.1. TOML About
13.1.1. SetUp
>>> import tomllib
13.1.2. From String
>>> data = """
... project = "myproject"
... version = "1.0.0"
... """
>>>
>>> data = tomllib.loads(data)
>>> data
{'project': 'myproject', 'version': '1.0.0'}
13.1.3. From File
project = "myproject"
version = "1.0.0"
>>> with open('/tmp/myfile.toml', mode='rb') as file:
... data = tomllib.load(file)
13.1.4. Conversion Table
TOML's
tableis Python'sdictTOML's
stringis Python'sstrTOML's
integeris Python'sintTOML's
floatis Python'sfloatTOML's
booleanis Python'sboolTOML's
offset datetimeis Python'sdatetime.datetime(tzinfoattribute set to an instance ofdatetime.timezone)TOML's
local datetimeis Python'sdatetime.datetime(tzinfoattribute set toNone)TOML's
local dateis Python'sdatetime.dateTOML's
local timeis Python'sdatetime.timeTOML's
arrayis Python'slist
13.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.*"]