如何使用 AWS SDK 从 YAML 文件到 Python 重复调用带有成对参数列表的函数?

how to repeatedly call a function with a list of paired arguments from YAML file to Python using AWS SDK?

我想编写一个脚本,使用 python boto 3 从 YAML 文件中获取 AWS 账户的值,并在 AWS 组织下创建多个账户。 请找到我要执行的以下步骤: 第 1 步: 我在 YAML 文件中列出了 AWS 账户的值,如下所示:(config.yaml)

Name:
   test1
   test2
Email:
    test1@gmail.com
    test2@gmail.com

第 2 步: 编写一个 python 脚本来自动化该过程

import yaml

with open("config.yml", 'r') as ymlfile:
    account = yaml.safe_load(ymlfile)

for section in cfg:
    print(section)
    print(account['Name'])
    print(account['Email'])

在我看来,您的配置文件看起来不对。有两个 "parallel" 列表很少是一个好主意(我想这是你的意图,即使破折号不见了)。我会给它这样的结构:

accounts:
- name: test1
  email: test1@gmail.com
- name: test2
  email: test2@gmail.com

并以类似这样的方式阅读它:

import yaml

with open("config.yml", 'r') as ymlfile:
    config = yaml.safe_load(ymlfile)
accounts = config['accounts']
for account in accounts:
    print()
    print(account['name'])
    print(account['email'])


更新

也许你需要做这样的事情?

# ...
for account in accounts:
    response = client.create_account(
        AccountName = account['name'],
        Email       = account['email'])

(boto3 的命名约定真是太棒了!)