如何使用Python将列表附加到DynamoDB表中?

如何使用Python将列表附加到DynamoDB表中?,python,python-3.x,python-2.7,amazon-dynamodb,amazon-dynamodb-local,Python,Python 3.x,Python 2.7,Amazon Dynamodb,Amazon Dynamodb Local,我有一个现有的DynamoDB表,我想编写一些Python代码,将一个属性(类型为List)附加到表中。以下是我尝试过的: users.put_item( Item={ "new_attribute": [] } ) 但这不起作用。我在网上到处找,但什么也找不到,我知道我肯定错过了一些基本的东西。任何帮助都将不胜感激。导入boto3 import boto3 dynamodb = boto3.resource('dynamodb')

我有一个现有的DynamoDB表,我想编写一些Python代码,将一个属性(类型为List)附加到表中。以下是我尝试过的:

users.put_item(

    Item={

        "new_attribute": []

    }

)
但这不起作用。我在网上到处找,但什么也找不到,我知道我肯定错过了一些基本的东西。任何帮助都将不胜感激。

导入boto3
import boto3

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('<your-ddb-table-name>')

table.update_item(
    Key={
        'PK': '<pk>',
        'SK': '<sk>'
    },
    UpdateExpression='SET new_attribute = :list',
    ExpressionAttributeValues={
        ':list': []
    }
)
dynamodb=boto3.resource('dynamodb') 表=发电机表(“”) 表1.1更新项目( 钥匙={ “PK”:“, “SK”:” }, UpdateExpression='SET new_属性=:list', 表达式属性值={ “:列表”:[] } )
下面是一个完整的示例

### Simulating an Insert and Update to a List #Create Table import boto3 dynamodb = boto3.resource('dynamodb') try: table = dynamodb.create_table( TableName='Test_list', KeySchema=[ { 'AttributeName': '_id', 'KeyType': 'HASH' # Partition key } ], AttributeDefinitions=[ { 'AttributeName': '_id', 'AttributeType': 'N' } ], ProvisionedThroughput={ 'ReadCapacityUnits': 5, 'WriteCapacityUnits': 5 } ) except ClientError as e: if e.response['Error']['Code']: print(e.response['Error']['Message']) print( e.response) ## Add a record with a list table= dynamodb.Table('Test_list') ll=['one','two'] resp=table.put_item( Item={ '_id': 1, 'mylist': ll } ) #Update the list new_ll=['three','four'] response = table.update_item( Key={ '_id': 1 }, UpdateExpression="SET #l = list_append(#l, :vals)", ExpressionAttributeNames={ "#l": 'mylist' }, ExpressionAttributeValues={ ":vals": new_ll } ) # fetch the record to verify resp=table.get_item(Key={'_id':1}) resp['Item']
我将代码粘贴到一个空白文件中,但收到以下错误消息:
botocore.exceptions.ClientError:调用UpdateItem操作时发生错误(ValidationException):提供的键元素与架构不匹配
{'_id': Decimal('1'), 'mylist': ['one', 'two', 'three', 'four']}