python 的配置文件使用 yaml

pip install pyyaml
# config.yaml
database:
  host: localhost
  username: myuser
  password: mypassword
# read_config.py
import yaml


def init_config_dict(config_file_path) -> dict:
    with open('config.yaml', 'r') as f:
        conf = yaml.safe_load(f)
    return conf


conf_file_name = "config.yaml"

config = init_config_dict(conf_file_name)

print(config)
print(config["database"]["host"])

猜你喜欢

转载自blog.csdn.net/wjl__ai__/article/details/131077695