for循环仅在python aws dynamodb中采用最后一个值

时间:2020-05-28 23:59:23

标签: python amazon-web-services amazon-dynamodb

我试图将记录插入表中,但是只有循环中的最后一条记录(结果数据)才插入表中

这是我尝试的代码:

fields

没弄错我在哪里

1 个答案:

答案 0 :(得分:0)

您在循环外分配了ResultData并在每次循环运行时更改了相同键的值。试试这个:

CDates = ['2020-05-10', '2020-05-12', '2020-05-13', '2020-05-16', '2020-05-20']

for date in CDates:
    filterDate = Key('Date').eq(id)
    appResponse = appTable.scan(FilterExpression=filterDate)
    accResp = table.query(
        KeyConditionExpression=Key('PrimaryId').eq('Key'),
        FilterExpression=Key('Date').eq(date))
    if len(accResp['Items']) == 0:
        ResultData = {
            'PrimaryId': 'Key',
            'CreationDate': date,
            'Type': 'Appt',
            'Id': str(uuid.uuid4())
        }
        print(ResultData)
        table.put_item(Item=ResultData)