boto的大多数输出都是字典或列表的形式(嵌套在其中的列表或字典)。
我如何处理它以访问输出的每个元素。
例如: 当我运行以下代码时:
import boto.rds2
dbInstance = 'dummydb'
region = 'ap-southeast-1'
conn = boto.rds2.connect_to_region(region)
db = conn.describe_db_instances(db_instance_identifier=dbInstance)
print db
输出结果为:
{u'DescribeDBInstancesResponse': {u'DescribeDBInstancesResult': {u'Marker': None, u'DBInstances': [{u'PubliclyAccessible': False, u'MasterUsername': u'horizon', u'LicenseModel': u'general-public-license', u'VpcSecurityGroups': [{u'Status': u'active', u'VpcSecurityGroupId': u'sg-xxxxxx'}, {u'Status': u'active', u'VpcSecurityGroupId': u'sg-xxxxxx'}], u'InstanceCreateTime': 123555, u'OptionGroupMemberships': [{u'Status': u'in-sync', u'OptionGroupName': u'default:mysql-5-6'}], u'PendingModifiedValues': {u'MultiAZ': None, u'MasterUserPassword': None, u'Port': None, u'Iops': None, u'AllocatedStorage': None, u'EngineVersion': None, u'BackupRetentionPeriod': None, u'DBInstanceClass': None, u'DBInstanceIdentifier': None}, u'Engine': u'mysql', u'MultiAZ': True, u'LatestRestorableTime': 1455555.0, u'DBSecurityGroups': [], u'DBParameterGroups': [{u'DBParameterGroupName': u'dummy-dbparametergroup', u'ParameterApplyStatus': u'pending-reboot'}], u'ReadReplicaSourceDBInstanceIdentifier': None, u'AutoMinorVersionUpgrade': True, u'PreferredBackupWindow': u'18:30-19:00', u'DBSubnetGroup': {u'VpcId': u'vpc-abcdef', u'Subnets': [{u'SubnetStatus': u'Active', u'SubnetIdentifier': u'subnet-abcdef', u'SubnetAvailabilityZone': {u'Name': u'ap-southeast-1a', u'ProvisionedIopsCapable': False}}, {u'SubnetStatus': u'Active', u'SubnetIdentifier': u'subnet-aa52e5dd', u'SubnetAvailabilityZone': {u'Name': u'ap-southeast-1b', u'ProvisionedIopsCapable': False}}], u'DBSubnetGroupName': u'dummysubnetgrp', u'SubnetGroupStatus': u'Complete', u'DBSubnetGroupDescription': u'DB Subnet Group'}, u'SecondaryAvailabilityZone': u'ap-southeast-1b', u'ReadReplicaDBInstanceIdentifiers': [], u'AllocatedStorage': 5, u'BackupRetentionPeriod': 7, u'DBName': u'horizon', u'PreferredMaintenanceWindow': u'sat:18:00-sat:18:30', u'Endpoint': {u'Port': 13306, u'Address': u'dummydb.c97xzgqljzmk.ap-southeast-1.rds.amazonaws.com'}, u'DBInstanceStatus': u'available', u'StatusInfos': None, u'EngineVersion': u'5.6.22', u'CharacterSetName': None, u'AvailabilityZone': u'ap-southeast-1a', u'Iops': None, u'DBInstanceClass': u'db.t2.micro', u'DBInstanceIdentifier': u'dummydb'}]}, u'ResponseMetadata': {u'RequestId': u'16786bd-4376-11e5-0b54-cf3492bd297a'}}}
现在我的要求是访问ParameterApplyStatus,以便我可以决定是否应该重启我的数据库。
我尝试通过更新打印句子来访问元素,如下所示:
print db["DescribeDBInstancesResponse"]["DescribeDBInstancesResult"]["DBInstances"]
但我得到的输出是:
[{u'PubliclyAccessible': False, u'MasterUsername': u'horizon', u'LicenseModel': u'general-public-license', u'VpcSecurityGroups': [{u'Status': u'active', u'VpcSecurityGroupId': u'sg-xxxxxx'}, {u'Status': u'active', u'VpcSecurityGroupId': u'sg-xxxxxx'}], u'InstanceCreateTime': 123555, u'OptionGroupMemberships': [{u'Status': u'in-sync', u'OptionGroupName': u'default:mysql-5-6'}], u'PendingModifiedValues': {u'MultiAZ': None, u'MasterUserPassword': None, u'Port': None, u'Iops': None, u'AllocatedStorage': None, u'EngineVersion': None, u'BackupRetentionPeriod': None, u'DBInstanceClass': None, u'DBInstanceIdentifier': None}, u'Engine': u'mysql', u'MultiAZ': True, u'LatestRestorableTime': 1455555.0, u'DBSecurityGroups': [], u'DBParameterGroups': [{u'DBParameterGroupName': u'dummy-dbparametergroup', u'ParameterApplyStatus': u'pending-reboot'}], u'ReadReplicaSourceDBInstanceIdentifier': None, u'AutoMinorVersionUpgrade': True, u'PreferredBackupWindow': u'18:30-19:00', u'DBSubnetGroup': {u'VpcId': u'vpc-abcdef', u'Subnets': [{u'SubnetStatus': u'Active', u'SubnetIdentifier': u'subnet-abcdef', u'SubnetAvailabilityZone': {u'Name': u'ap-southeast-1a', u'ProvisionedIopsCapable': False}}, {u'SubnetStatus': u'Active', u'SubnetIdentifier': u'subnet-aa52e5dd', u'SubnetAvailabilityZone': {u'Name': u'ap-southeast-1b', u'ProvisionedIopsCapable': False}}], u'DBSubnetGroupName': u'dummysubnetgrp', u'SubnetGroupStatus': u'Complete', u'DBSubnetGroupDescription': u'DB Subnet Group'}, u'SecondaryAvailabilityZone': u'ap-southeast-1b', u'ReadReplicaDBInstanceIdentifiers': [], u'AllocatedStorage': 5, u'BackupRetentionPeriod': 7, u'DBName': u'horizon', u'PreferredMaintenanceWindow': u'sat:18:00-sat:18:30', u'Endpoint': {u'Port': 13306, u'Address': u'dummydb.c97xzgqljzmk.ap-southeast-1.rds.amazonaws.com'}, u'DBInstanceStatus': u'available', u'StatusInfos': None, u'EngineVersion': u'5.6.22', u'CharacterSetName': None, u'AvailabilityZone': u'ap-southeast-1a', u'Iops': None, u'DBInstanceClass': u'db.t2.micro', u'DBInstanceIdentifier': u'dummydb'}]}
如何进一步细分,以便我可以访问输出中的每个元素。
答案 0 :(得分:1)
你在那里的最后一个对象是一个实例列表,所以你需要处理该列表中的每个实例,看起来输出只包含1个实例。
您应该能够执行以下操作:
import boto.rds2
dbInstance = 'dummydb'
region = 'ap-southeast-1'
conn = boto.rds2.connect_to_region(region)
db = conn.describe_db_instances(db_instance_identifier=dbInstance)
instances = print db["DescribeDBInstancesResponse"]["DescribeDBInstancesResult"]["DBInstances"]
for instance in instances:
if 'ParameterApplyStatus' in instance['DBParameterGroups'][0]:
print instance['DBParameterGroups'][0]['ParameterApplyStatus']
如果有帮助,你可以在输出中使用类似JSON lint的东西,看看如何最好地访问结构中的元素