繁体   English   中英

For 循环只取列表中的最后一个元素

[英]For loop is taking only the last element in the list

我很抱歉问了一个简单的问题,但尝试了多个选项但没有任何解决方案。 我遇到的问题是 for 循环只取列表(区域)中的最后一个值。


import boto3
import csv
import io
from io import BytesIO
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart


ses = boto3.client('ses')
email_from = 'example@gmail.com'
email_to = 'example@gmail.com'
email_cc = 'example@gmail.com'
emaiL_subject = 'Unused_Security'
email_body = '***Link to S3 Object***'


def lambda_handler(event, context):
    regions = ['eu-west-1','eu-west-2','us-east-2','us-west-1','us-west-2','us-east-1']
    for region in regions:
        csvio = io.BytesIO()
        writer = csv.writer(csvio)
        writer.writerow([
            'Account Name',
            'Region',
            'Id'
        ])
        ec2 = boto3.resource('ec2', region_name=region)
        sgs = list(ec2.security_groups.all())
        insts = list(ec2.instances.all())
        all_sgs = set([sg.group_id for sg in sgs])
        all_inst_sgs = set([sg['GroupId'] for inst in insts for sg in
        inst.security_groups])
        unused_sgs = all_sgs - all_inst_sgs
        for elem in unused_sgs:
            writer.writerow([
                Account_Name,
                region,
                elem
                ])
        s3 = boto3.client('s3')
        s3.put_object(Body=csvio.getvalue(), ContentType='application/vnd.ms-excel', Bucket='unused-sg', Key='Unused_Security.csv', ACL='public-read') 

        csvio.close()
        s3.get_object(Bucket='unused-sg', Key='Unused_Security.csv') 
    response = ses.send_email(
        Source = email_from,
        Destination={
            'ToAddresses': [
                email_to,
            ],
            'CcAddresses': [
                email_cc,
            ]
        },
        Message={
            'Subject': {
                'Data': emaiL_subject
            },
            'Body': {
                'Text': {
                    'Data': email_body
                }
            }
        }
    )

这是列表,它只需要最后一个值


regions = ['eu-west-1','eu-west-2','us-east-2','us-west-1','us-west-2','us-east-1']

它必须获取列表(区域)中的所有值并显示结果。 但它只需要最后一个值(us-east-1)。 请指教是什么导致了错误。

这里:

for region in regions:
    csvio = io.BytesIO()
    writer = csv.writer(csvio)
    # ...
    s3.put_object(Body=csvio.getvalue(), ContentType='application/vnd.ms-excel', Bucket='unused-sg', Key='Unused_Security.csv', ACL='public-read') 

您正在为每个区域创建一个新的 csv 文件,覆盖前一个。 你想把它排除在外:

csvio = io.BytesIO()
writer = csv.writer(csvio)
for region in regions:
    # massage the data 
    # write to the csv
    # ...

# now we're outside the loop we write the full thing
s3 = boto3.client('s3')
s3.put_object(Body=csvio.getvalue(), ContentType='application/vnd.ms-excel', Bucket='unused-sg', Key='Unused_Security.csv', ACL='public-read') 
csvio.close()

您正在覆盖此行中每个循环的 csv 文件s3.put_object(Body=csvio.getvalue(), ContentType='application/vnd.ms-excel', Bucket='unused-sg', Key='Unused_Security.csv', ACL='public-read') 把它放在 for 循环之外,所以你只能在循环完成后写入 s3

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM