调用PutItem操作时出错(ValidationException):从CSV文件更新DynamoDB时

2024-04-20 01:08:05 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是csv

1,'Chaks, Raj','$300,000', False
2,'Chen, Joe','$250,000', False
3,'Kumar, Harry','$240,000', True

下面是代码

import boto3
import csv
def lambda_handler(event, context):
    region='us-east-1'
    recList=[]
    try:            
        s3=boto3.client('s3')            
        dyndb = boto3.client('dynamodb', region_name=region)
        confile= s3.get_object(Bucket='csvfolder3', Key='employee.csv')
        recList = confile['Body'].read().decode('utf-8').split('\n')
        firstrecord=True
        csv_reader = csv.reader(recList, delimiter=',', quotechar='"')
        for row in csv_reader:
            if (firstrecord):
                firstrecord=False
                continue
            empid = row[0]
            name = row[1].replace(',','').replace('$','') if row[1] else '-'
            salary = row[2].replace(',','').replace('$','') if row[2] else 0
            response = dyndb.put_item(
                TableName='emplist',
                Item={
                'empid' : {'N':str(empid)},
                'name': {'S':name},
                'salary': {'N':str(salary)},
                'parttime': {'BOOL':False},
                }
            )
        print('Put succeeded:')
    except Exception as e:
        print (str(e))

下面是引发的错误:

a bytes-like object is required, not 'str'

从csv更新到DynamoDB时

错误:

An error occurred (ValidationException) when calling the PutItem operation: The parameter cannot be converted to a numeric value: Joe


Tags: csvnamefalseifs3boto3regionreplace
1条回答
网友
1楼 · 发布于 2024-04-20 01:08:05

问题在于你的引号

该文件包含:

1,'Chaks, Raj','$300,000', False
2,'Chen, Joe','$250,000', False
3,'Kumar, Harry','$240,000', True

但是,您告诉csv_reader引号是双引号:

csv_reader = csv.reader(recList, delimiter=',', quotechar='"')

因此,只需将其更改为:

csv_reader = csv.reader(recList, delimiter=',', quotechar="'")

相关问题 更多 >