logo
Published on

python3.11以降で.tomlファイルを扱う方法

Authors

python3.11 で.toml ファイルを読み込む関数が実装されました。

実装例

  • .toml をこんな感じに作ってみました。
[user]
name="snuow"
password = "0000"
mail = "snuow.tech@gmail.com"

[machine]
os='windows'
cpu='intel'
memory='32GB'
  • 例えば、user の name を読み込みたい場合は下記のようにします。
import tomllib # tomlファイルのパーサー

with open("./toml_settings.toml","rb") as f:
    tom = tomllib.load(f)

print(tom['user']['name'])