【问题标题】:how to create a json string to pass it ot create_table()如何创建一个 json 字符串以将其传递给 create_table()
【发布时间】:2017-04-19 17:06:26
【问题描述】:

对于boto的create_table函数,我想要下面的字符串

'AttributeDefinitions': [{'AttributeName': 'Id','AttributeType': 'N'},{'AttributeName': 'orgId','AttributeType': 'S'}]

'KeySchema': [{'AttributeName': 'orgId','KeyType': 'HASH'},{'AttributeName': 'Id','KeyType': 'RANGE'}]

'ProvisionedThroughput': {'ReadCapacityUnits': 1,'WriteCapacityUnits': 1}

我想用变量替换值,我尝试了以下方法但没有成功。

attribute_definition="'AttributeDefinitions': [{'AttributeName':"+hash_attribute_name+",'AttributeType':"+hash_attribute_type+"},{'AttributeName': "+range_attribute_name+",'AttributeType': "+range_attribute_type+"}]"

key_schema="'KeySchema': [{'AttributeName': "+hash_attribute_name+",'KeyType': 'HASH'},{'AttributeName': "+hash_attribute_type+",'KeyType': 'RANGE'}]"

capacity="'ProvisionedThroughput': {'ReadCapacityUnits': "+read_capacity+",'WriteCapacityUnits': "+write_capacity+"}"

然后我尝试创建如下列表。

attribute_definition={'AttributeDefinitions': [{'AttributeName':hash_attribute_name,'AttributeType':hash_attribute_type},{'AttributeName': range_attribute_name,'AttributeType': range_attribute_type}]}

key_schema={'KeySchema': [{'AttributeName': hash_attribute_name,'KeyType': 'HASH'},{'AttributeName': hash_attribute_type,'KeyType': 'RANGE'}]}

capacity={'ProvisionedThroughput': {'ReadCapacityUnits': read_capacity,'WriteCapacityUnits': write_capacity}

它抛出以下错误:print ("attribute_definition") ^ SyntaxError: invalid syntax

我也尝试使用 json.loads() 将字符串转换为 json

attribute_definition=json.loads("{'AttributeDefinitions': [{'AttributeName':"+hash_attribute_name+",'AttributeType':"+hash_attribute_type+"},{'AttributeName': "+range_attribute_name+",'AttributeType': "+range_attribute_type+"}]}")

key_schema=json.loads("{'KeySchema': [{'AttributeName': "+hash_attribute_name+",'KeyType': 'HASH'},{'AttributeName': "+hash_attribute_type+",'KeyType': 'RANGE'}]}")

capacity=json.loads("{'ProvisionedThroughput': {'ReadCapacityUnits': "+read_capacity+",'WriteCapacityUnits': "+write_capacity+"}}")

发现以下错误:ValueError: Expecting property name: line 1 column 2 (char 1)

我想将此字符串或列表传递给 create_table 函数以创建表。

谁能帮我实现它。

【问题讨论】:

    标签: python json boto


    【解决方案1】:

    你试过 json.dumps 吗?

        >>> hash_attribute_name = 'Id'
        >>> hash_attribute_type = 'N'
        >>> range_attribute_name = 'orgId'
        >>> range_attribute_type = 'S'
        >>> attribute_definition={'AttributeDefinitions':[{'AttributeName':hash_attribute_name,'AttributeType':hash_attribute_type},{'AttributeName': range_attribute_name,'AttributeType': range_attribute_type}]}
        >>> attribute_definition
        {'AttributeDefinitions': [{'AttributeName': 'Id', 'AttributeType': 'N'}, {'AttributeName': 'orgId', 'AttributeType': 'S'}]}
        >>> import json
        >>> json.dumps(attribute_definition)
        '{"AttributeDefinitions": [{"AttributeName": "Id", "AttributeType": "N"}, {"AttributeName": "orgId", "AttributeType": "S"}]}'
    

    【讨论】:

    • 它不起作用:(,它仍然没有将其转换为列表。
    猜你喜欢
    • 2021-08-30
    • 1970-01-01
    • 2019-11-16
    • 2018-11-25
    • 1970-01-01
    • 2019-06-04
    • 1970-01-01
    • 1970-01-01
    • 2021-07-04
    相关资源
    最近更新 更多