boto3 TypeError: 'NoneType' object is not iterable
boto3 TypeError: 'NoneType' object is not iterable
我正在尝试使用 boto3 打印我调用 'Function' 的 EC2 标签的值,但在代码运行大约 20 个对象后在第 7 行收到 TypeError: 'NoneType' object is not iterable
错误。我试图通过检查该值是否为 None
来捕获此错误,但仍然收到此错误。
import boto3
s = boto3.Session(profile_name='default')
ec2 = s.resource('ec2')
for i in ec2.instances.all():
for tag in i.tags:
if tag['Value'] is None:
print("No Value")
else:
if tag['Key'] == 'Function':
print(tag['Value'])
您可以尝试这样做并检查:
def tag_to_dict(ec2):
tag_dict = {}
if ec2.tags is None:
return "No Value" ## Replace "No Value" with tag_dict to get an empty dict
for tag in ec2.tags:
tag_dict[tag['Key']] = tag['Value']
return tag_dict
你最终得到了一个字典,其中键作为键,标记值作为值。
编辑:
试试看看你能不能在这里得到任何东西:
for i in ec2.instances.all():
if i.tags is None:
continue
for tag in i.tags:
if tag['Key'] == 'Function':
print(tag['Value'])
它继续循环的下一次迭代并忽略 NoneType
个值。
我正在尝试使用 boto3 打印我调用 'Function' 的 EC2 标签的值,但在代码运行大约 20 个对象后在第 7 行收到 TypeError: 'NoneType' object is not iterable
错误。我试图通过检查该值是否为 None
来捕获此错误,但仍然收到此错误。
import boto3
s = boto3.Session(profile_name='default')
ec2 = s.resource('ec2')
for i in ec2.instances.all():
for tag in i.tags:
if tag['Value'] is None:
print("No Value")
else:
if tag['Key'] == 'Function':
print(tag['Value'])
您可以尝试这样做并检查:
def tag_to_dict(ec2):
tag_dict = {}
if ec2.tags is None:
return "No Value" ## Replace "No Value" with tag_dict to get an empty dict
for tag in ec2.tags:
tag_dict[tag['Key']] = tag['Value']
return tag_dict
你最终得到了一个字典,其中键作为键,标记值作为值。
编辑:
试试看看你能不能在这里得到任何东西:
for i in ec2.instances.all():
if i.tags is None:
continue
for tag in i.tags:
if tag['Key'] == 'Function':
print(tag['Value'])
它继续循环的下一次迭代并忽略 NoneType
个值。