TOMLしたい(tomllib

1import tomllib
2
3fname = Path("設定ファイル.toml")
4with fname.open("rb") as f:
5    config = tomllib.load(f)

tomllibはPython3.11で標準モジュールに追加された読み込み専用のTOMLパーサーです。 TOML形式のファイルを読み込み、辞書型に変換します。

キー/バリューしたい

1integer = 1
2string = "two"
3number = 3.0
1{
2    "integer": 1,
3    "string": "two",
4    "number": 3.0,
5}

リストしたい

1integers = [1, 2, 3]
2strings = ["one", "two", "three"]
3mixed = [1, "two", 3.0]
1{
2    "integers": [1, 2, 3],
3    "strings": ["one", "two", "three"],
4    "mixed": [1, "two", 3.0],
5}

インライン・テーブルしたい

1[[record]]
2run_id = 1
3distance = 10.0}
4
5[[record]]
6run_id = 2
7distance = 20.0
1{"record":
2    [
3        {"run_id": 1, "distance": 10.0},
4        {"run_id": 2, "distance": 20.0},
5    ]
6}

リファレンス