【发布时间】:2022-01-15 23:44:07
【问题描述】:
我有以下 template.yaml 文件:
Resources:
ApiGatewayDeployment:
Type: AWS::ApiGateway::Deployment
Properties:
RestApiId: ApiGateway
当我尝试使用以下 python 代码解析它时:
import pathlib
import yaml
def main():
template_file = pathlib.Path('template.yaml')
cfn = yaml.safe_load(template_file.read_text())
for res in cfn["Resources"]:
print(res)
if __name__ == "__main__":
main()
我正在获取密钥作为输出:
ApiGatewayDeployment
但是当我使用下面的代码解析它时:
import pathlib
import yaml
def main():
template_file = pathlib.Path('template.yaml')
cfn = yaml.safe_load(template_file.read_text())
for res in cfn["Resources"],:
print(res)
if __name__ == "__main__":
main()
我将字典作为输出:
{'ApiGatewayDeployment': {'Type': 'AWS::ApiGateway::Deployment', 'Properties': {'RestApiId': 'ApiGateway'}}}
谁能解释一下这个逻辑?
编辑:更新了第二个 python 代码
【问题讨论】:
-
感谢@iain-shelvington 的指点。我已经更新了它。输出的差异只是因为 for 循环中的逗号(,)
标签: python dictionary yaml