Python boto实例。输出问题

Python boto实例。输出问题,python,amazon-ec2,amazon-web-services,instance,boto,Python,Amazon Ec2,Amazon Web Services,Instance,Boto,更新: 我按照下面的建议做了,但是,我仍然得到一个关键错误:输出中的“Name”,即使输出是正确的。 这就是输出: 输出为hoky tho: us-west-1 EC2Connection:ec2.us-west-1.amazonaws.com Showing all of your current instances i-xxxxxxxx: {u'Name': u'Proxy-xx-1000-Enrollments'} i-xxxxxxxx: {u'Name

更新:


我按照下面的建议做了,但是,我仍然得到一个关键错误:输出中的“Name”,即使输出是正确的。

这就是输出:

输出为hoky tho:

us-west-1
EC2Connection:ec2.us-west-1.amazonaws.com
Showing all of your current instances
    i-xxxxxxxx:     {u'Name': u'Proxy-xx-1000-Enrollments'}
    i-xxxxxxxx:     {u'Name': u'Proxy-xx-1000-Enrollments'}
    i-xxxxxxxx:     {u'Name': u'Proxy-xx-1000-Enrollments'}
    i-xxxxxxxx:     {u'Name': u'Proxy-xx-1000-Enrollments'}
    i-xxxxxxxx:     {u'Name': u'Proxy-xx-1000-Enrollments'}
当我将
\uuuu dict\uuu
方法更改为仅“拉出”名称时,它可以工作(或者似乎可以工作,但会抛出一个错误:

以下是代码更改:

print“\t%s:\t%s”%(instance.id,instance.\u dict\u['tags']['Name'])
以下是输出:

us-west-1
EC2Connection:ec2.us-west-1.amazonaws.com
Showing all of your current instances
    i-xxxxxxxx:     Proxy-xx-1000-Enrollments
    i-xxxxxxxx:     Proxy-xx-1000-Enrollments
    i-xxxxxxxx:     Proxy-xx-1000-Enrollments
    i-xxxxxxxx:     Proxy-xx-1000-Enrollments
    i-xxxxxxxx:     Proxy-xx-1000-Enrollments
    i-xxxxxxxx:     Proxy-xx-1000-Enrollments

Traceback (most recent call last):
  File "/Users/xxxxxx/xxx.xxx/boto/instanceid.py", line 43, in <module>
    print "\t%s: \t%s" % (instance.id, instance.__dict__['tags']["Name"])
KeyError: 'Name'
us-west-1
ec2连接:ec2.us-west-1.amazonaws.com
显示所有当前实例
i-xxxxxxxx:Proxy-xx-1000-注册
i-xxxxxxxx:Proxy-xx-1000-注册
i-xxxxxxxx:Proxy-xx-1000-注册
i-xxxxxxxx:Proxy-xx-1000-注册
i-xxxxxxxx:Proxy-xx-1000-注册
i-xxxxxxxx:Proxy-xx-1000-注册
回溯(最近一次呼叫最后一次):
文件“/Users/xxxxxx/xxx.xxx/boto/instanceid.py”,第43行,在
打印“\t%s:\t%s”%(instance.id,instance.\uu dict\uuu['tags'][“Name”])
KeyError:“名称”
我更喜欢这个输出,没有错误-有人能告诉我我做错了什么吗


谢谢

为什么不直接访问tags属性呢?通过
\uuu dict\uuu
访问它并不是一种很好的方式:

instance.tags['Name']
您可能还需要查看实例的状态,因为从get_all_instances()返回的保留对象中的某些实例可能是最近终止的实例。您可以仅报告正在运行的实例的名称,否则报告状态(注意-此.state检查只是显示
A if cond else B
语法的一个想法。您仍然需要自己使用它):

如果您只想避免KeyError并返回默认值,则可以使用

instance.tags.get('Name')  
# or with a default
instance.tags.get('Name', '--')
下面是从中提取的一个blurb,其中提到了保留对象和实例状态

如果您只想获得所有正在运行的实例的列表,请使用 连接对象的get_all_instances方法 返回的列表实际上是保留对象的列表(包含 实例),并且该列表可能包括最近终止的 实例在其终止后的一小段时间内


为什么不直接访问tags属性呢?通过
\uuuu dict\uuuuu
访问它不是一种很好的方式:

instance.tags['Name']
您可能还需要查看实例的状态,因为从get_all_instances()返回的保留对象中的某些实例可能是最近终止的实例。您可以仅报告正在运行的实例的名称,否则报告状态(注意-此.state检查只是显示
A if cond else B
语法的一个想法。您仍然需要自己使用它):

如果您只想避免KeyError并返回默认值,则可以使用

instance.tags.get('Name')  
# or with a default
instance.tags.get('Name', '--')
下面是从中提取的一个blurb,其中提到了保留对象和实例状态

如果您只想获得所有正在运行的实例的列表,请使用 连接对象的get_all_instances方法 返回的列表实际上是保留对象的列表(包含 实例),并且该列表可能包括最近终止的 实例在其终止后的一小段时间内


谢谢你的回答,非常感谢。但是,当我使用你的语法时,我仍然会得到“keyrerror”运行更新的代码时:
us-west-1 EC2Connection:ec2.us-west-1.amazonaws.com显示所有当前实例代理-44-1000-Enrollmations Proxy-45-1000-Enrollmations Proxy-48-1000-Enrollmations Proxy-49-1000-Enrollmations Proxy-59-1000-Enrollmations Proxy-67-1000-Enrollmations Proxy-70-1000-Enrollmations Proxy-71-1000-Enrollmations Proxy-73-1000-Enrollments Traceback(最近一次调用):文件“/Users/xxxxx/xxxx/boto/instanceid.py”,第43行,打印“\t%s%”(instance.tags['Name'])如果instance.state=='running'else instance.state keyrorm:'Name'
谢谢你的回答,非常感谢。但是,当我使用你的语法时,我仍然会得到“keyrorm”运行更新的代码时:
us-west-1 EC2Connection:ec2.us-west-1.amazonaws.com显示所有当前实例代理-44-1000-Enrollmations Proxy-45-1000-Enrollmations Proxy-48-1000-Enrollmations Proxy-49-1000-Enrollmations Proxy-59-1000-Enrollmations Proxy-67-1000-Enrollmations Proxy-70-1000-Enrollmations Proxy-71-1000-Enrollmations Proxy-73-1000-Enrollments Traceback(最近一次调用):文件“/Users/xxxxx/xxxx/boto/instanceid.py”,第43行,打印“\t%s%”(instance.tags['Name']),如果instance.state=='running'else instance.state KeyError:'Name'