遍历dict列表以创建多个dynamodb条目会创建一个条目,然后停止

2024-06-28 19:58:28 发布

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

我正在使用dynamodb的本地实例进行练习,从测试实例中获取项目,并将json对象放回本地表中

例如,查询一个id作为主键会给我一个“Items”响应,其中包含四个dict(在测试数据库中确认了四条记录):

def getRecordsById(id, table):

    response = table.query(
        KeyConditionExpression=Key('recordId').eq(id)
    )
    return response

答复:

{'Items': [{'processId': 'a1',
   'recommendation': {'value': Decimal('0'),
    'processor': 'John Doe'},
   'createDate': '2019-02-27T18:53:16.380Z',
   'status': 'rejected',
   'recordId': '5',
   'createdBy': 'Buddy',
   'actions': [{'actor': 'Buddy',
     'actionType': 'review',
     'note': '444551233344',
     'actorName': 'Buddy',
     'createDate': '2019-02-27T18:53:16.380Z'},
    {'actor': 'Johnny',
     'actionType': 'reject',
     'note': '234',
     'actorName': 'Johnny',
     'createDate': '2019-02-27T19:00:46.425Z'}],
   'type': 'adjusted'},
  {'processId': 'a2',
   'recommendation': {'value': Decimal('0'),
    'processor': 'John Doe'},
   'createDate': '2019-02-27T18:53:16.380Z',
   'status': 'rejected',
   'recordId': '5',
   'createdBy': 'Buddy',
   'actions': [{'actor': 'Buddy',
     'actionType': 'review',
     'note': '444551233344',
     'actorName': 'Buddy',
     'createDate': '2019-02-27T18:53:16.380Z'},
    {'actor': 'Johnny',
     'actionType': 'reject',
     'note': '234',
     'actorName': 'Johnny',
     'createDate': '2019-02-27T19:00:46.425Z'}],
   'type': 'adjusted'},
  {'processId': 'a3',
   'recommendation': {'value': Decimal('0'),
    'processor': 'John Doe'},
   'createDate': '2019-02-27T18:53:16.380Z',
   'status': 'rejected',
   'recordId': '5',
   'createdBy': 'Buddy',
   'actions': [{'actor': 'Buddy',
     'actionType': 'review',
     'note': '444551233344',
     'actorName': 'Buddy',
     'createDate': '2019-02-27T18:53:16.380Z'},
    {'actor': 'Johnny',
     'actionType': 'reject',
     'note': '234',
     'actorName': 'Johnny',
     'createDate': '2019-02-27T19:00:46.425Z'}],
   'type': 'adjusted'},
  {'processId': 'a4',
   'recommendation': {'value': Decimal('0'),
    'processor': 'John Doe'},
   'createDate': '2019-02-27T18:53:16.380Z',
   'status': 'rejected',
   'recordId': '5',
   'createdBy': 'Buddy',
   'actions': [{'actor': 'Buddy',
     'actionType': 'review',
     'note': '444551233344',
     'actorName': 'Buddy',
     'createDate': '2019-02-27T18:53:16.380Z'},
    {'actor': 'Johnny',
     'actionType': 'reject',
     'note': '234',
     'actorName': 'Johnny',
     'createDate': '2019-02-27T19:00:46.425Z'}],
   'type': 'adjusted'}],
 'Count': 4,
 'ScannedCount': 4,
 'ResponseMetadata': {'RequestId': '4LI4AVRIAQ',
  'HTTPStatusCode': 200,
  'HTTPHeaders': {'server': 'Server',
   'date': 'Thu, 03 Oct 2019 19:45:59 GMT',
   'content-type': 'application/x-amz-json-1.0',
   'content-length': '13934',
   'connection': 'keep-alive',
   'x-amzn-requestid': '4LI4AVR',
   'x-amz-crc32': '4101956423'},
  'RetryAttempts': 0}}

然后,我将其精简为一个目录:

trimmed = blurb_from_test["Items"]

然后,我使用put_项迭代此过程:

for i in range(len(trimmed)):
    table_local.put_item(
        Item=trimmed[i]
    )

但我只得到一条记录(使用上面相同的getRecordsByd方法):

{'Items': [{'processId': 'a4',
   'recommendation': {'value': Decimal('0'),
    'processor': 'John Doe'},
   'createDate': '2019-02-27T18:53:16.380Z',
   'status': 'rejected',
   'recordId': '5',
   'createdBy': 'Buddy',
   'actions': [{'actor': 'Buddy',
     'actionType': 'review',
     'note': '444551233344',
     'actorName': 'Buddy',
     'createDate': '2019-02-27T18:53:16.380Z'},
    {'actor': 'Johnny',
     'actionType': 'reject',
     'note': '234',
     'actorName': 'Johnny',
     'createDate': '2019-02-27T19:00:46.425Z'}],
   'type': 'adjusted'}]
 'Count': 1,
 'ScannedCount': 1,
 'ResponseMetadata': {'RequestId': 'ced4a02e-f878-4edf-843a-2aabfadb0133',
  'HTTPStatusCode': 200,
  'HTTPHeaders': {'content-type': 'application/x-amz-json-1.0',
   'x-amz-crc32': '4260213483',
   'x-amzn-requestid': 'ced4a02e-f878-4edf-843a-2aabfadb0133',
   'content-length': '3360',
   'server': 'Jetty(8.1.12.v20130726)'},
  'RetryAttempts': 0}}

我的假设是,我没有正确地对put_项使用迭代,但我还没有找到任何原因


Tags: valuetypejohnprocessornoteactordecimalbuddy
1条回答
网友
1楼 · 发布于 2024-06-28 19:58:28

您可以按如下方式进行迭代:

for item in response.get('Items'):
    print(item)

可根据您的目的更改打印报表,即

for item in response.get('Items'):
    table_local.put_item(
        Item=item
    )

相关问题 更多 >