python - Unpack uncertain number of attributes from iterator object in Boto DynamoDB Scan -


so using aws dynamodb because of nosql-ness , ability have ambiguous number of 'columns' speak. using boto interface database, getting different number of columns/attributes dynamically proving difficult.

my db table without definitive schema (why i'm going nosql) , rows have attributes different other rows. know dynamo can this, need way keys/columns/attributes simple scan. database not large , not grow @ i'm not worried efficiency of scan/query.

my table (more or less):

{'name': 'john',    'email': '12@34.com'} {'name': 'charlie', 'email': '34@56.com', 'dislikes': 'people's knees'} {'name': 'joe',     'email': '78@90.com', 'hobby':    'golf'} 

as can see, there different attributes each row.

my testing script boto

import os import boto.dynamodb2 boto.dynamodb2.table import table  aws_access_key_id = os.getenv("aws_access_key_id") aws_secret_access_key = os.getenv("aws_secret_access_key")   def connect():     conn = boto.dynamodb2.connect_to_region(         'us-east-1',         aws_access_key_id=aws_access_key_id,         aws_secret_access_key=aws_secret_access_key)     return conn  conn = connect()  table = table('table1', connection=conn)  scan = table.scan() 

this returns iterator object

<boto.dynamodb2.items.item object @ ....> 

to parse through object, 1 must know attribute names , object cannot indexed numbers:

for in scan:     print i['name']     # john     # charlie     # joe  in scan:     print i[0]     # none     # none     # none 

and when use multiple variables unpacking, works, have define variables this:

for i, j, k in scan:         print i, j, k 

which works rows 3 columns three.

what want able loop through each row , corresponding columns back, different next rows. have hit wall , feedback appreciated.

found problem!

so feel pretty dumb. key part of answer .items() called on scan. allowed scan iterated on in way wanted to.

i wanted dictionary of various-sized dictionaries needed create wider-scoped dictionary hold each row/person , more narrow scope add each detail (key/column , value). data structure might different mine gets pointed in right direction.

...  scan = table.scan()  #outer-most dict each row dict0 = {}  # enumerate each row can appended dict0 using index  index, row in enumerate(scan):     # inner-most dict each key/value pair     dict1 = {}     key, value in row.items():         dict1[str(key)] = str(value)     dict0[index] = dict1 

i used str() because getting unicode u' ' characters in results.

iterating on data same, we're leaving out dictionary stuff.

for index, keyvalues in dict0.items():     print index     key, value in keyvalues.items():         print "\t {}: {}".format(key, value) 

prints out (sorted time added):

0     "hobby": "golf"     "email": "78@90.com"     "name" : "joe" 1     'dislikes': "people's knees"     "name" : "charlie"     "email": "34@56.com" 2     "name" : "john"     "email": "12@34.com" 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -