对于那里的一些 python 专业人士来说,这可能真的是微不足道的问题,但我正在使用 boto3 来获取一些快照信息...... VolumeId”,我认为这是一个关键值输出,我可以使用一些值 rs.value 来获得它,但我没有得到想要的输出......
>>> import boto3
>>> client = boto3.client('ec2')
>>> rs = client.describe_snapshots(SnapshotIds=['snap-656f5566'])
>>> print rs
{'ResponseMetadata': {'HTTPStatusCode': 200, 'RequestId': '6f99cc31-f586-48cf-b9bd-f5ca48a536fe'}, u'Snapshots': [{u'Description': 'Created by CreateImage(i-bbe81dc1) for ami-28ne0f44 from vol-72e14126', u'Encrypted': False, u'VolumeId': 'vol-41e14536', u'State': 'completed', u'VolumeSize': 30, u'Progress': '100%', u'StartTime': datetime.datetime(2012, 10, 7, 14, 33, 16, tzinfo=tzlocal()), u'SnapshotId': 'snap-658f5566', u'OwnerId': '0111233286342'}]}
>>>
>>>
>>> dir(rs)
['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
>>>
>>> print rs.keys
<built-in method keys of dict object at 0x1e76a60>
>>>
>>> print rs.values
<built-in method values of dict object at 0x1e76a60>
>>>
修复后出错
>>> print rs.keys()
['ResponseMetadata', u'Snapshots']
>>> print(rs['ResponseMetadata']['Snapshots'][0]['VolumeId'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'Snapshots'
>>>