convert output to list(and nested dictionary)

Peter Otten __peter__ at web.de
Thu Jul 23 11:59:21 CEST 2015


max scalf wrote:

> Hi Peter,
> 
> Could you please explain what i am doing wrong?  I did inspected the
> "get_all_security_groups()" object using dir and i do need the get_data
> function for this to work...as i have to parse the output...just getting
> the rule and grants does not work...as it comes with extra verbiage that i
> do NOT need in my dictionary...see below...
> 
>>>> for sg in sgs:
>     for rule in sg.rules:
>         print sg, sg.id, rule, rule.grants
> 
> 
> SecurityGroup:wordpress-app-SG sg-99c4befc IPPermissions:-1(None-None)
> [sg-e632d982-995635159130]
...

It's not "extra verbiage", you print the whole sg object, and the author of 
the SecurityGroup class has chosen one way to convert SecurityGroup 
instances into strings, the one that he deems most useful, probably for 
debugging purposes.

But you are only interested in some attributes of the sg object. Instead of

print sg

you should only print

print sg.foo, sg.bar

foo and bar are fictitious attribute names, but you can find candiates for 
these attributes with

print dir(sg)

which will print the actual names.



More information about the Python-list mailing list