我有一个YAML文件,即test.yml
,该文件如下所示:
details:
server1: abc.host.com
port: 80
我正在尝试读取这些键并使用带有代码的Python分配给字符串:
#!/usr/bin/python
import os
import yaml
with open('test.yml', 'r') as cfg:
yamlcfg=yaml.load(cfg)
str1 = "Hostname: " + (yamlcfg['details']['server1'])
print str1
运行此代码时。我收到错误消息:
TypeError: list indices must be integers, not str
我在做什么错了?