Python小记之 yaml配置文件

import yaml
import os

config_file = "config.yaml"
test_dict = {"a": 1, "b": 2}

if not os.path.exists(config_file):
    with open(config_file, "w", encoding="utf8") as w:
        pass

with open(config_file, "w", encoding="utf-8") as w:
    yaml.dump(test_dict, w)

with open(config_file, 'r', encoding='utf8') as f:
    config = f.read()
    yaml_data = yaml.safe_load(config)
    print(yaml_data)

posted @ 2022-09-22 18:10  肖越  阅读(103)  评论(0)    收藏  举报