上传json到DynamoDB获取字符串

2024-09-28 12:14:22 发布

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

我有Json文件包含以下行:

{
  "tenant_EntityID": {
    "s": "12345"
  },
  "enrtyDate": {
    "s": "7.9.2000 14:53:45"
  },
  "first_name": {
    "s": "Y7M9"
  },
  "last_name": {
    "s": "NUYE"
  },
  "gender": {
    "s": "male"
  },
  "birth_incorp_date": {
    "s": "9.3.1999 14:49:44"
  },
  "email": {
    "s": "0XPY9E@C20R.com"
  }
}

当我试图通过以下代码将其加载到DynamoDB时:

^{pr2}$

我得到了一个错误:

Traceback (most recent call last):
  File "C:/Freedom/Comparing json file.py", line 39, in <module>
    'tenant_EntityID':Entity['tenant_EntityID'] ,
TypeError: string indices must be integers

Tags: 文件namejsondategendermalefirstlast
1条回答
网友
1楼 · 发布于 2024-09-28 12:14:22

当您将JSON字符串读入实体时,结果是一个dict,带有键"tenant_EntityID"等等。语句for Entity in Entities在该dict上迭代,给出dict的键,即字符串。在

看起来你需要这样的东西:

import boto3
import json
import decimal

dynamodb = boto3.resource('dynamodb', region_name='us-west-2')

table = dynamodb.Table('Entities')

with open("C:/data/bigJson1.json") as json_file:
    Entity = json.load(json_file, parse_float = decimal.Decimal)
    table.put_item(
        Item={
            'tenant_EntityID':Entity['tenant_EntityID'] ,
            'enrtyDate': Entity['enrtyDate']['s'],
            'first_name': Entity['first_name']['s'],
            'last_name': Entity['last_name']['s'],
            'gender': Entity['gender']['s'],
            'birth_incorp_date': Entity['birth_incorp_date']['s'],
            'email': Entity['email']
            }
        )

我猜想您需要与's'键关联的值。在

你说“当你用2行运行”时它失败了,很可能JSON应该被转换成一个字典列表,而不是一个字典。在

相关问题 更多 >

    热门问题