Python 3.x 无效的类型参数类str,有效的类型类dict

Python 3.x 无效的类型参数类str,有效的类型类dict,python-3.x,boto3,Python 3.x,Boto3,所以我尝试使用put_项同步Dynamodb表。但我遇到了一个问题 Invalid type for parameter Item.Artist, value: No One You Know, type: <class 'str'>, valid types: <class 'dict'> 为了正确添加它 这是我的密码: #!/usr/bin/python3 import boto3 source = boto3.resource('dynamodb', 'us-ea

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

Invalid type for parameter Item.Artist, value: No One You Know, type: <class 'str'>, valid types: <class 'dict'>
为了正确添加它

这是我的密码:

#!/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)
因此,如果我取消对print语句的注释,我会得到:

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

是否有任何方法来清理输出或添加额外的必需的'S',或者我的做法是错误的?

在代码的某个阶段,您将有
item={'Artist':'No One One you knower','songtail':'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


在代码的某个阶段,您将拥有
item={'Artist':'No One One you knower','songtail':'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


非常感谢。成功了。我不确定是否也应该使用boto3.resource作为dest,但效果非常好。我很感激。太棒了!!很乐意帮忙…谢谢!成功了。我不确定是否也应该使用boto3.resource作为dest,但效果非常好。我很感激。太棒了!!很乐意帮助。。。