在此答案中,描述了如何使用python编辑yaml文件的特定条目。我尝试将代码用于以下yaml文件
- fields: {domain: "www.mydomain.com", name: "www.mydomain.com"}
model: sites.site
pk: 1
但随着
with open('my.yaml', 'r') as f:
doc = yaml.load(f)
txt = doc["fields"]["domain"]
print txt
我得到了错误
Traceback (most recent call last):
File "./test.py", line 9, in <module>
txt = doc["fields"]["domain"]
TypeError: list indices must be integers, not str
现在,我以为我可以使用doc
..有人可以帮助我吗?:)
您获取的数据是一个列表。
改变
txt = doc["fields"]["domain"]
到
txt = doc[0]["fields"]["domain"]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句