# 先安装ruamel.yaml模块 写入配置文件:
import os# 先安装ruamel.yaml模块from ruamel import yaml # 将字典写入到yamldict = { ‘host1‘: ‘123‘, ‘host2‘: ‘456‘, ‘host3‘: ‘789‘, ‘host4‘: ‘147‘, ‘host5‘: {‘asd‘: ‘123‘} } curpath = os.path.dirname(os.path.realpath(__file__))yamlpath = os.path.join(curpath, "config.yaml") # 写入到yaml文件with open(yamlpath, "w", encoding="utf-8") as f: yaml.dump(dict, f, Dumper=yaml.RoundTripDumper) 读取配置文件
# 读取yaml文件# a = open(yamlpath, "r")# print(yaml.load(a.read(), Loader=yaml.Loader)) a = yaml.load(open(yamlpath, "r").read(), Loader=yaml.Loader)print(a)print(a[‘host1‘])
原文地址:https://www.cnblogs.com/zhongyehai/p/9642008.html
时间: 2024-10-31 13:20:40