使用 Boto3 在 AWS DynamoDB 中插入地图列表 (python 3)
Inserting List of Maps in AWS DyanmoDB using Boto3 (python 3)
teamMembers = []
for member in team:
teamMembers.append({'M' : {
'id' : {'S' : member['id']},
'role' : {'S' : member['role']},
'name' : {'S' : member['name']}
}})
project = {
'id' : {
'S' : data['project']['id']
},
'name' : {
'S' : data['project']['name']
},
'team' : {
'L' : teamMembers
}
}
client.put_item(
TableName = 'projects',
Item = project
)
此代码出现以下错误:
[ERROR] TypeError: string indices must be integers
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 36, in lambda_handler
'id' : {'S' : member['id']},
错误消息表明您的 team
不完全正确。它似乎是一个包含字典和字符串的列表,如下所示:
: team = [
{'id' : 'hekspkh', 'role' : 'admin', 'name' : 'Devin' },
{'id' : 'hekspkh2', 'role' : 'admin2', 'name' : 'Devin2' },
'some_string',
{'id' : 'hekspkh3', 'role' : 'admin3', 'name' : 'Devin3' }
]
因此你必须仔细检查你的 team
并过滤掉那些破坏你代码的字符串。
teamMembers = []
for member in team:
teamMembers.append({'M' : {
'id' : {'S' : member['id']},
'role' : {'S' : member['role']},
'name' : {'S' : member['name']}
}})
project = {
'id' : {
'S' : data['project']['id']
},
'name' : {
'S' : data['project']['name']
},
'team' : {
'L' : teamMembers
}
}
client.put_item(
TableName = 'projects',
Item = project
)
此代码出现以下错误:
[ERROR] TypeError: string indices must be integers
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 36, in lambda_handler
'id' : {'S' : member['id']},
错误消息表明您的 team
不完全正确。它似乎是一个包含字典和字符串的列表,如下所示:
: team = [
{'id' : 'hekspkh', 'role' : 'admin', 'name' : 'Devin' },
{'id' : 'hekspkh2', 'role' : 'admin2', 'name' : 'Devin2' },
'some_string',
{'id' : 'hekspkh3', 'role' : 'admin3', 'name' : 'Devin3' }
]
因此你必须仔细检查你的 team
并过滤掉那些破坏你代码的字符串。