键数据预期类型不匹配:S实际:L Boto3与Dynamodb插入错误

2024-04-25 21:51:32 发布

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

我有以下脚本来将json文件插入DynamoDb表:

import os
import boto3
import json

region = '<>'
image = 'ami-<>'
ubuntu_image = 'ami-<>'
keyname = '<>'
AWS_ACCESS_KEY_ID = '<>'
AWS_SECRET_ACCESS_KEY = '<>'


def dynamo(path):
    session = boto3.Session(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
    dynamo = session.resource('dynamodb')
    table = dynamo.Table('<>')

    for filename in os.listdir(path):
        with open(path + '/' + filename) as data_file:
            data = json.loads(data_file.read())
            print data
            table.put_item(
                           Item={
                                'filename': filename,
                                'data': data,
                            }
                        )      

        print filename



print dynamo('<>')

尝试执行此操作时出现以下错误:

^{pr2}$

其中一个json文件是:

[{"text": "Vasovagal syncope", "type": "DX_LSTM", "end": 103, "assertion": 1, "start": 86}, {"text": "Traumatic arthritis", "type": "DX_LSTM", "end": 145, "assertion": 1, "start": 126}, {"text": "right knee", "type": "DX_LSTM", "end": 157, "assertion": 1, "start": 147}, {"text": "Hypertension", "type": "DX_LSTM", "end": 174, "assertion": 1, "start": 162}, {"text": "urinary tract infection", "type": "DX_LSTM", "end": 223, "assertion": 1, "start": 200}, {"text": "renal carcinoma", "type": "DX_LSTM", "end": 254, "assertion": 1, "start": 239}, {"text": "obstructive pulmonary disease", "type": "DX_LSTM", "end": 315, "assertion": 1, "start": 286}, {"text": "stroke", "type": "DX_LSTM", "end": 442, "assertion": 1, "start": 436}, {"text": "hypertension", "type": "DX_LSTM", "end": 456, "assertion": 1, "start": 444}, {"text": "COPD", "type": "DX_LSTM", "end": 462, "assertion": 1, "start": 458}, {"text": "renal carcinoma", "type": "DX_LSTM", "end": 487, "assertion": 1, "start": 472}, {"text": "syncope", "type": "DX_LSTM", "end": 533, "assertion": 1, "start": 526}, {"text": "fell to her knees", "type": "DX_LSTM", "end": 584, "assertion": 1, "start": 567}, {"text": "hit her head on the ground, near", "type": "DX_LSTM", "end": 625, "assertion": 1, "start": 593}, {"text": "loss of consciousness", "type": "DX_LSTM", "end": 725, "assertion": 0, "start": 704}, {"text": "falls", "type": "DX_LSTM", "end": 804, "assertion": 1, "start": 799}, {"text": "hip fracture", "type": "DX_LSTM", "end": 845, "assertion": 1, "start": 833}, {"text": "bruising around the left eye", "type": "DX_LSTM", "end": 967, "assertion": 1, "start": 939}, {"text": "decreased mobility of", "type": "DX_LSTM", "end": 1085, "assertion": 1, "start": 1064}, {"text": "left", "type": "DX_LSTM", "end": 1094, "assertion": 1, "start": 1090}, {"text": "syncope", "type": "DX_LSTM", "end": 1175, "assertion": 0, "start": 1168}, {"text": "stroke", "type": "DX_LSTM", "end": 1195, "assertion": 0, "start": 1189}, {"text": "fractures", "type": "DX_LSTM", "end": 1348, "assertion": 0, "start": 1339}, {"text": "left humeral head", "type": "DX_LSTM", "end": 1405, "assertion": 0, "start": 1388}, {"text": "neck fracture", "type": "DX_LSTM", "end": 1423, "assertion": 0, "start": 1410}, {"text": "baseline anterior dislocation", "type": "DX_LSTM", "end": 1458, "assertion": 1, "start": 1429}, {"text": "changes", "type": "DX_LSTM", "end": 1500, "assertion": 0, "start": 1493}, {"text": "left periorbital soft tissue swelling", "type": "DX_LSTM", "end": 1539, "assertion": 0, "start": 1502}, {"text": "soft tissue", "type": "DX_BLME", "end": 1530, "assertion": 0, "start": 1519}, {"text": "facial bone fracture", "type": "DX_LSTM", "end": 1600, "assertion": 0, "start": 1580}, {"text": "ventricular function", "type": "DX_LSTM", "end": 1656, "assertion": 1, "start": 1636}, {"start": 1774, "end": 1782, "text": "syncopal", "type": "DX_LSTM", "assertion": 0}, {"text": "orthostatic", "type": "DX_LSTM", "end": 1865, "assertion": 1, "start": 1854}, {"text": "walk", "type": "DX_LSTM", "end": 2021, "assertion": 1, "start": 2017}, {"text": "traumatic injury of her knee", "type": "DX_LSTM", "end": 2072, "assertion": 1, "start": 2044}, {"text": "injury", "type": "DX_Exact", "end": 2060, "assertion": 1, "start": 2054}, {"text": "pain", "type": "DX_LSTM", "end": 2098, "assertion": 1, "start": 2094}, {"text": "swelling", "type": "DX_LSTM", "end": 2111, "assertion": 1, "start": 2103}, {"text": "fractures", "type": "DX_LSTM", "end": 2154, "assertion": 0, "start": 2145}, {"text": "frail", "type": "DX_LSTM", "end": 2175, "assertion": 0, "start": 2170}]

Tags: keytextawsjsondataaccesstypefilename
1条回答
网友
1楼 · 发布于 2024-04-25 21:51:32

首先,看起来您正在尝试将List对象持久化为DynamoDB的String数据类型。在

其次,字符串属性data-我认为它被定义为Key属性。键属性不能是列表数据类型。因此,不能将键属性更改为List。在

我不知道你想达到什么目标。但是,我想您应该将文件中的整个JSON存储在其中一个属性上。在

如果您尝试将数据存储在某个非键属性中,则上述代码应该可以工作。在

输出将是一个List属性,所有对象(即单个实例)都存储为Map。在

见示例下图:-

enter image description here

相关问题 更多 >