AWS BotoCore 错误 - AttributeValue 不能包含空字符串

AWS BotoCore Error - An AttributeValue may not contain an empty string

我正在尝试用旧事件存储中的数据填充 DynamoDB 数据库 由 PostgreSQL 数据库组成。 运行 通过大部分数据库条目后,在尝试调用 put_item 函数时抛出此错误。

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

我决定重新运行代码,并通过在插入之前转储所有 table 属性来查看发生了什么。

我可以看到唯一的 "empty string" 在字典的 answer_string 属性中,称为 details,见下文:-

Importing event type 5 completed by user: 1933
1933 5 {'answer': {'difficulty': 1, 'answer_string': ''}, 'card_id': n 
'13448', 'review_id': '153339', 'time_spent': 2431}
62 153339
2017-01-18 00:46:48.373009+00:00 2017-01-18 00:46:48.364217+00:00

我很确定这是导致错误的原因,因为 none 的其他 table 属性不正确。

我的问题是 details 字典可能来自许多不同的位置,每个 details 字典可能有不同的属性 - 具有 answer_string 属性的字典只是其中之一可能的字典配置。我不可能检查字典的所有可能配置并验证它们都没有空字符串。

有没有一种方法可以对字典进行一次全面检查,看看其中是否有任何一部分是空的?

如果你想得到一个只包含所有空值键的字典,你可以简单地应用一个 dictionary comprehensiondetails-dict 来获得所有空值的键值对。例如:

empty_values = {key: value for key, value in details.items() if not value}

如果您想过滤掉具有空值的键值对,那么您会得到一个所有键都有值的字典,只需使用没有 not 的相同理解:

details = {key: value for key, value in details.items() if value}

或者,如果您想用 None 值替换所有空字符串:

def removeEmptyString(dic):
    for e in dic:
        if isinstance(dic[e], dict):
            dic[e] = removeEmptyString(dic[e])
        if (isinstance(dic[e], str) and dic[e] == ""):
            dic[e] = None
        if isinstance(dic[e], list):
            for entry in dic[e]:
                removeEmptyString(entry)
    return dic

dictionaryWithEmptyStringsReplacedWithNone = removeEmptyString(dicrionaryWithEmptyStrings)

它远非完美,但它确实有效。

@PedoDorf 的功能对我有用,尽管我必须添加一个检查,因为有时它会 return "TypeError: string indices must be integers" 在收到字符串时

def removeEmptyString(dic):
  if isinstance(dic, str):
    if dic == "":
      return None
    else:
      return dic

  for e in dic:
    if isinstance(dic[e], dict):
      dic[e] = removeEmptyString(dic[e])
    if (isinstance(dic[e], str) and dic[e] == ""):
      dic[e] = None
    if isinstance(dic[e], list):
      for entry in dic[e]:
        removeEmptyString(entry)
  return dic

谢谢

如果您需要考虑嵌套对象并同时清理它们,请尝试一下。需要一些递归:

def clean_ddb_data(obj):
    cleaned = {}
    for k, v in obj.items():
        if isinstance(v, dict):
            cleaned[k] = clean_ddb_data(v)
        elif isinstance(v, str):
            if len(v) > 0:
                cleaned[k]=v
        else:
            cleaned[k]=v
    return cleaned