python从for循环中提取值到另一个for循环中

时间:2020-10-25 13:10:45

标签: python for-loop boto3

我正在使用boto3并使用for循环来获取所有竞价请求及其各自的实例ID。然后,它将检查这些实例,以查看它们是否正在运行,停止或挂起,并且如果没有这些实例则必须终止,因此我希望它取消链接的竞价请求。

我遇到的问题是我无法弄清楚如何将输出的竞价型实例ID放入下一个for循环中,以检查该实例是否正在运行。到目前为止,我的代码是:

 #!/usr/bin/python3
import boto3
import pprint
ec2 = boto3.client('ec2', 'eu-west-1')
spot_requests = ec2.describe_spot_instance_requests()
describe_instance = ec2.describe_instances()

for index in spot_requests['SpotInstanceRequests']:
    instanceID = index["InstanceId"]
    instanceState = index["State"]
    spotRequestID = index["SpotInstanceRequestId"]
    if instanceState != 'active':

            print(f'This instance {instanceID} is {instanceState}')

            for item in instanceID:
                    if item["instanceID"],item["State"] == "Running" or item["instanceID"],item["State"] == "Stopped" or item["instanceID"],item["State"] == "Pending"
                            print(f'{instanceID} is not terminated so do not cancel spot request {spotRequestID}')

                    else
                            print(f'Instance is terminated. Cancel spot request {spotRequestID}')
    else:
            print(f'{instanceID} is {instanceState} and has a request ID of {spotRequestID}')

在此行之前效果很好

print(f'This instance {instanceID} is {instanceState}')

但是我不能从这里继续,因为它不断给我我不理解的错误。我不确定逗号是否正确,但是似乎找不到明确的答案。任何帮助都会很棒,谢谢

0 个答案:

没有答案