如何通过忽略bot3中的空元素将JSON数据写入Dynamodb

2024-06-07 01:29:58 发布

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

我想将以下数据组写入Dynamodb。
大约有100个数据。由于不一定需要图像,因此可以混合使用或不使用图像url元素。

(questionsList.json)

{
  "q_id" : "001",
  "q_body" : "Where is the capital of the United States?",
  "q_answer" : "Washington, D.C.",
  "image_url" : "/Washington.jpg",
  "keywords" : [
    "UnitedStates",
    "Washington"
  ]
},
{
  "q_id" : "002",
  "q_body" : "Where is the capital city of the UK?",
  "q_answer" : "London",
  "image_url" : "",
  "keywords" : [
    "UK",
    "London"
  ]
},

由于是写入测试阶段,要写入的Dynamodb在localhost:8000中使用的是无服务器框架的无服务器Dynamodb本地插件,而不是生产环境。
为了将上述JSON数据写入这个Dynamodb,我在Boto 3(awsdkforpython)中编写了以下代码

from __future__ import print_function
import boto3
import codecs
import json

dynamodb = boto3.resource('dynamodb', region_name='us-east-1', endpoint_url="http://localhost:8000")

table = dynamodb.Table('questionListTable')

with open("questionList.json", "r", encoding='utf-8') as json_file:
items = json.load(json_file)
for item in items:
    q_id = item['q_id']
    q_body = item['q_body']
    q_answer = item['q_answer']
    image_url = item['image_url']
    keywords = item['keywords']

    print("Adding detail:", q_id, q_body)

    table.put_item(
        Item={
            'q_id': q_id,
            'q_body': q_body,
            'q_answer': q_answer,
            'image_url': image_url,
            'keywords': keywords,
        }
    )

执行此代码时,空字符部分出现以下错误。

botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the PutItem operation: One or more parameter values were invalid: An AttributeValue may not contain an empty string

显然,这似乎是由JSON的空字符引起的 如果您将包含空字符的图像url从下面的写入目标中排除,则写入操作将毫无问题地完成。

from __future__ import print_function
import boto3
import codecs
import json

dynamodb = boto3.resource('dynamodb', region_name='us-east-1', endpoint_url="http://localhost:8000")

table = dynamodb.Table('questionListTable')

with open("questionList.json", "r", encoding='utf-8') as json_file:
items = json.load(json_file)
for item in items:
    q_id = item['q_id']
    q_body = item['q_body']
    q_answer = item['q_answer']
    #image_url = item['image_url']
    keywords = item['keywords']

    print("Adding detail:", q_id, q_body)

    table.put_item(
        Item={
            'q_id': q_id,
            'q_body': q_body,
            'q_answer': q_answer,
            #'image_url': image_url,
            'keywords': keywords,
        }
    )

由于DynamoDB是NoSQL,可能还有其他方法可以很好地利用这些特性,但是如何更正代码以忽略空字符来编写上述数据呢?我想说“如果图像url存在,请编写它,如果不存在,请忽略它。”

谢谢您。


Tags: the数据answer图像imageimportidjson
1条回答
网友
1楼 · 发布于 2024-06-07 01:29:58

我解决了我的问题。您可以如下设置空值。

from __future__ import print_function
import boto3
import codecs
import json

dynamodb = boto3.resource('dynamodb', region_name='ap-northeast-1', endpoint_url="http://localhost:8000")

table = dynamodb.Table('questionListTable')

with open("questionList.json", "r", encoding='utf-8_sig') as json_file:
    items = json.load(json_file)
    for item in items:
    q_id = item['q_id']
    q_body = item['q_body']
    q_answer = item['q_answer']
    image_url = item['image_url'] if item['image_url'] else None
    keywords = item['keywords'] if item['keywords'] else None

    print("Adding detail:", q_id, q_body)

    table.put_item(
        Item={
            'q_id': q_id,
            'q_body': q_body,
            'q_answer': q_answer,
            'image_url': image_url,
            'keywords': keywords,
        }
    )

为了检查Dynamodb的情况,使用无服务器框架的离线插件在本地环境中运行API网关。当我实际使用Postman调用API时,在值中正确地插入了Null。

{
  "q_id" : "001",
  "q_body" : "Where is the capital of the United States?",
  "q_answer" : "Washington, D.C.",
  "image_url" : "/Washington.jpg",
  "keywords" : [
    "UnitedStates",
    "Washington"
  ]
},
{
  "q_id" : "002",
  "q_body" : "Where is the capital city of the UK?",
  "q_answer" : "London",
  "image_url" : "null",
  "keywords" : [
    "UK",
    "London"
  ]
},

相关问题 更多 >