Below is the csv
1,'Chaks, Raj','$300,000', False
2,'Chen, Joe','$250,000', False
3,'Kumar, Harry','$240,000', True
Below is the code
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))
Below is the error thrown:
a bytes-like object is required, not 'str'
while updating from csv to DynamoDB
Error:
An error occurred (ValidationException) when calling the PutItem operation: The parameter cannot be converted to a numeric value: Joe
The problem is with your quotation marks.
The file contains:
1,'Chaks, Raj','$300,000', False
2,'Chen, Joe','$250,000', False
3,'Kumar, Harry','$240,000', True
However, you are telling csv_reader
that the quotes are double-quotes:
csv_reader = csv.reader(recList, delimiter=',', quotechar='"')
So, simply change it to:
csv_reader = csv.reader(recList, delimiter=',', quotechar="'")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.