从DynamoDB获取错误序列顺序的数据

时间:2016-10-19 10:53:43

标签: python amazon-web-services amazon-dynamodb

我在从DynamoDB下载数据时遇到问题。我尝试使用Python SDK以及AWS CLI(aws dynamodb scan --table-name Alarms),但每次都遇到同样的问题。有没有人知道造成这种情况的原因。

输出获取

    {
        "FRE": {
            "S": "1"
        },
        "MB": {
            "S": "0"
        },
        "TW": {
            "S": "1"
        },
        "FNB": {
            "S": "0"
        },
        "Date": {
            "S": "2016-10-19 09:04:47.083456"
        },
        "TD2": {
            "S": "1"
        },
        "TD1": {
            "S": "1"
        },
        "TB": {
            "S": "1"
        }
    }

需要输出

    {
         "Date": {
            "S": "2016-10-19 09:04:47.083456"
        },

        "FRE": {
            "S": "1"
        },
        "MB": {
            "S": "0"
        },
        "TW": {
            "S": "1"
        },
        "FNB": {
            "S": "0"
        },
        "TD2": {
            "S": "1"
        },
        "TD1": {
            "S": "1"
        },
        "TB": {
            "S": "1"
        }
    }

由于 Waqas Ali Khan

1 个答案:

答案 0 :(得分:0)

当你创建字典对象时,它有维护顺序的问题,它不会按照添加到其中的元素的顺序进行迭代。

#So we have to create Ordered dict you can use collection package as follow's 

from collections import OrderedDict
data_dict = OrderedDict() 
data_dict["Date"] = {'S': '2016-10-19 09:04:47.083456'}
data_dict["FRE"] = {'S': '1'}
data_dict["MB"] = {'S': '0'}
data_dict["TW"] = {'S': '1'}
data_dict["FNB"] = {'S': '0'}
data_dict["TD2"] = {'S': '1'}
data_dict["TD1"] = {'S': '1'}
data_dict["TB"] = {'S': '1'}

output:
#It maintain order of data inserted:
print data_dict
OrderedDict([('Date', {'S': '2016-10-19 09:04:47.083456'}), ('FRE', {'S': '1'}), ('MB', {'S': '0'}), ('TW', {'S': '1'}), ('FNB', {'S': '0'}), ('TD2', {'S': '1'}), ('TD1', {'S': '1'}), ('TB', {'S': '1'})])

现在,您将按照其中添加的数据顺序维护目录的顺序。你也可以按顺序迭代。