Nested loops in Python and CSV file

时间秒杀一切 提交于 2020-06-29 03:49:13

问题


I have a python lambda with nested for loop

def lambda_handler(event, context):
    acc_ids = json.loads(os.environ.get('ACC_ID'))
    with open('/tmp/newcsv.csv', mode='w',  newline='') as csv_file:
        fieldnames = ['DomainName', 'Subject', 'Status', 'RenewalEligibility', 'InUseBy']
        writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
        writer.writeheader()
        for acc_id in acc_ids:
            try:
                //do something
                for region in regions_to_scan:
                    try:
                       // do something
                        if something:
                            for x in list:
                                // get values for row
                                writer.writerow('write rows here')
                        
                    except Exception as e:
                        print(e)
                        continue 
            except Exception as e:
                print(e)
                continue
    s3 = boto3.client('s3')
    response = s3.upload_file('/tmp/newcsv.csv', 'my-bucket', 'newcsv.csv')

This is not uploading the file in S3, from other conversations I made sure that the uploading of the file is after the writing context is done. Can someone evaluate my script?


回答1:


Turns out the loop and the lambda were correct. The timeout on the lambda function was set to 10 secs and it never reached the code to upload the file. Increasing the timeout on lambda fixed my problem



来源:https://stackoverflow.com/questions/62542225/nested-loops-in-python-and-csv-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!