小编uhi*_*ama的帖子

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

我想将以下数据组写入 Dynamodb。
大约有100条数据。由于不一定需要图像,因此可以混合使用和不使用 image_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"
  ]
},
Run Code Online (Sandbox Code Playgroud)

由于是写测试阶段,要写的Dynamodb是使用serverless框架的serverless-dynamodb-local插件在localhost:8000准备的,不是生产环境。
为了将上面的 JSON 数据写入这个 Dynamodb,我在 Boto 3 (AWS SDK for Python) 中编写了以下代码。

from __future__ import print_function
import boto3
import …
Run Code Online (Sandbox Code Playgroud)

python json amazon-web-services amazon-dynamodb boto3

5
推荐指数
1
解决办法
8706
查看次数