在这个answer中描述了如何用python编辑yaml文件的特定条目。我试图用代码以下YAML文件用Python编辑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
使用键..有人能帮助我吗? :)
完美!谢谢:) – TheWaveLad 2014-12-03 09:13:43
你可以'打印类型(doc)'来找出数据结构的类型。如果它只是简单的内置插件,'print doc'就足以让你看到数据是如何嵌套的。 – huggie 2014-12-03 09:16:40