Fail to get csv from S3 and convert it with Python

后端 未结 1 1736
闹比i
闹比i 2020-12-12 06:26

I need to read csv file from s3 bucket and insert each row on dynamoDB

def load_users_dynamodb():

s3 = boto3.client(\'s3\')
dynamodb = boto3.resource(\'dyna         


        
相关标签:
1条回答
  • 2020-12-12 06:38

    Your issue related to decoding the object return from s3.You need to read the file as csv.

    Take a look at the following code snippet:

        import boto3
        import csv
    
        s3 = boto3.client('s3')
    
        def lambda_handler(event, context):
            obj = s3.get_object(Bucket='Bucket_Name', Key='File_Name.csv')
            data = obj['Body'].read().decode('utf-8').splitlines()
            lines = csv.reader(data)
            headers = next(lines)
            print('headers: %s' %(headers))
            for line in lines:
                print(line)
    

    Output :

    Dummy csv.

    0 讨论(0)
提交回复
热议问题