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'sdict
TOML's
string
is Python'sstr
TOML's
integer
is Python'sint
TOML's
float
is Python'sfloat
TOML's
boolean
is Python'sbool
TOML's
offset datetime
is Python'sdatetime.datetime
(tzinfo
attribute set to an instance ofdatetime.timezone
)TOML's
local datetime
is Python'sdatetime.datetime
(tzinfo
attribute set toNone
)TOML's
local date
is Python'sdatetime.date
TOML's
local time
is Python'sdatetime.time
TOML's
array
is Python'slist
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.*"]