簡體   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