Search code examples
pythonamazon-dynamodbresultset

Code breakes while reading resultset returned by DyanmoDb's table.scan() in python


I'm really new at dynamoDB, so question might be very stupid. My purpose and code is very simple:

  1. I connect to DynamoDB.
  2. I get desired Table back from dynamoDB
  3. I read all values via Table.scan()
  4. but when i try to run for loop on returned resultset obtained at step 3 instead of iterating through resulset I fail with following error: TypeError: 'ResultSet' object has no attribute '__getitem__'

Here is my code:

     def fireQuery(tableName):
         table = getTable(tableName)
         try :
              result = table.scan(Select="ALL_ATTRIBUTES")
              for item in result['Items'] :  #<-- my code fails here
                  print(item)

     def getTable(tableName):
         try:
              db = getDynamoDBConnection()
              table = Table(tableName, connection=db)        
         except Exception as e:
              print(tableName ," Table doesn't exist.", e)
         return table

Exception on console:

for item in users['Items'] :

TypeError: 'ResultSet' object has no attribute '__getitem__'


Solution

  • You just need to iterate the ResultSet like this:

    def fireQuery(tableName):
        table = getTable(tableName)
        try :
             result_set = table.scan(Select="ALL_ATTRIBUTES")
             for item in result_set:
                 print item['attributename']