【问题标题】:python boto3 - how to get the value from the return of a functionpython boto3 - 如何从函数的返回中获取值
【发布时间】:2018-01-23 09:46:01
【问题描述】:

使用 python boto3 库,我使用的是 ec2.create_instances 函数,它返回如下所示的数据:

[ec2.Instance(id='i-34304930394309409')]

如何简单地提取 id 的值并将其放入另一个变量中?我只想检索“i-34304930394309409”并将其设置为名为“instanceid”的变量

有没有办法只调用 ec2.instance 来检索这个值?

【问题讨论】:

    标签: python amazon-ec2 boto3


    【解决方案1】:

    仅供参考:instance 是 create_instances() 的 boto3 函数返回的响应

    这就是我最终解决问题的方式:

    我创建了一个名为 instance_id 的变量,然后将其设置为 instance[0].id,这是列表中与 id 字符串匹配的第一个值。不确定我是否解释正确......无论如何:

    instance_id = instance[0].id
    print "Instance ID:", instance_id
    

    然后打印我想要检索的值:“i-34304930394309409”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多