python-3.x - 无效类型参数类 str,有效类型类 dict

标签 python-3.x boto3

所以我尝试使用 put_item 同步 Dynamodb 表。但我遇到了一个问题。

Invalid type for parameter Item.Artist, value: No One You Know, type: <class 'str'>, valid types: <class 'dict'>

从阅读文档来看,使用 put_item 时它需要一个字典,所以本质上是这样的:

'{'Artist': {'S': 'No One You Know'},'SongTitle': {'S': 'Call Me Today'}}'

为了正确添加它。

这是我的代码:

#!/usr/bin/python3
import boto3
source = boto3.resource('dynamodb', 'us-east-1')
dest = boto3.client('dynamodb', 'us-west-2')

def sync(source, dest):
     table = source.Table("myMusic")
     scan_kwargs = {
         'ProjectionExpression': "Artist, SongTitle"
     }
     done = False
     start_key = None
     while not done:
         if start_key:
             scan_kwargs['ExclusiveStartKey'] = start_key
         response = table.scan(**scan_kwargs)
         for item in response['Items']:
             dest.put_item(TableName="myMusic", Item=item)
             #print(item)
         start_key = response.get('LastEvaluatedKey', None)
         done = start_key is None


sync(source, dest)

因此,如果我取消注释打印语句,我会得到:

{'Artist': 'No One You Know', 'SongTitle': 'Call Me Today'}

是否有任何方法可以清理输出或添加额外所需的“S”,或者我是否以错误的方式处理此问题?

最佳答案

在代码的某个阶段,您将拥有 item = {'Artist': 'No One You Know', 'SongTitle': 'Call Me Today'} 当您取消注释 print 语句时,您所说的内容将会被打印。

使用以下代码片段:

newItem = { 'Artist': {}, 'SongTitle': {} }

newItem['Artist']['S'] = item['Artist']
newItem['SongTitle']['S'] = item['SongTitle']

所以整个代码变成:

#!/usr/bin/python3
import boto3
source = boto3.resource('dynamodb', 'us-east-1')
dest = boto3.client('dynamodb', 'us-west-2')

def sync(source, dest):
     table = source.Table("myMusic")
     scan_kwargs = {
         'ProjectionExpression': "Artist, SongTitle"
     }
     done = False
     start_key = None
     while not done:
         if start_key:
             scan_kwargs['ExclusiveStartKey'] = start_key
         response = table.scan(**scan_kwargs)
         for item in response['Items']:
             
#######################  SNIPPET  #######################  
             newItem = { 'Artist': {}, 'SongTitle': {} }

             newItem['Artist']['S'] = item['Artist']
             newItem['SongTitle']['S'] = item['SongTitle']
#######################  SNIPPET  #######################  

             dest.put_item(TableName="myMusic", Item=newItem)
             #print(item)
         start_key = response.get('LastEvaluatedKey', None)
         done = start_key is None

关于python-3.x - 无效类型参数类 str,有效类型类 dict,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67287259/

相关文章:

python-3.x - Pandas - 将具有不同名称和列数的数据帧堆叠在一起

python-3.x - 沃森 Python SDK : 'DetailedResponse' object is not subscriptable

python - 对 AWS Athena 查询结果进行分页时如何跳过 header

python - boto3 资源(如 DynamoDB.Table)的类型注释

python - 如何使用 XPath 通过其同级节点的属性来发现 XML 节点?

python-3.x - 无法在 python 中迭代 Azure ADGroupPaged 对象

python - 为什么使用 id() 时用整数分配变量总是恒定的?

django - 如何使用 django-storages 和 boto3 获取 aws s3 对象键

python - 如何使用具有特定 AWS 配置文件的 dask 从 s3 读取 Parquet 文件

amazon-web-services - boto3 copy vs copy_object 关于 s3 中的文件权限 ACL