【发布时间】:2019-03-20 01:54:22
【问题描述】:
我有一个结构如下的 yaml 文件:
这是 test.yaml
server:
- host: ubuntu
ip: 10.20.30.40
path: /var/log/syslog
- host: ubuntu
ip: 10.20.3.50
path: /var/log/syslog
当我调用我的 python 脚本时,我会加载 yaml 文件并解析内容。我无法迭代所有内容:
如何创建以下格式的列表:
import yaml
def read_yaml(file):
with open(file, "r") as stream:
try:
config = yaml.load(stream)
print("inside the function")
# print(config)
except yaml.YAMLError as exc:
print(exc)
print("\n")
return config
d = read_yaml("config.yaml")
print('{host}@{ip}:{path}'.format(*d['server']))
我想要 yaml 文件中不同服务器的 host@ip:path 输出。但是上面的命令不起作用并产生错误:TypeError: format() argument after ** must be a mapping, not list 要么 KeyError:主机
请帮忙。
【问题讨论】: