如何处理python中来自字典的键的错误键和空值
试图处理python中来自字典的键的错误键和空值。以下是所有如何处理python中来自字典的键的错误键和空值,python,Python,试图处理python中来自字典的键的错误键和空值。以下是所有资源都有标记,但有些没有。我试图按如下方式处理无值,但不断出现错误 我尝试的是: tags_info = [] resource_list = client.resources.list() for item in (resource_list): tags = item.tags managed_tag_value = tags['managed'] if not managed_tag_val
资源
都有标记
,但有些没有。我试图按如下方式处理无值,但不断出现错误
我尝试的是:
tags_info = []
resource_list = client.resources.list()
for item in (resource_list):
tags = item.tags
managed_tag_value = tags['managed']
if not managed_tag_value:
tags_info = {"Managed": "Not Managed"}
else:
tags_info = {"Managed": managed_tag_value}
print(tags_info)
managed_tag_value = tags['managed']
TypeError: 'NoneType' object is not subscriptable
{'name': '1111name', 'Managed': 'Not Managed'}
{'name': '2222name', 'Managed': 'notbillable'}
{'name': 'BastionRDP/MicrosoftMonitoringAgent', 'Managed': 'Not Managed'}
错误:
tags_info = []
resource_list = client.resources.list()
for item in (resource_list):
tags = item.tags
managed_tag_value = tags['managed']
if not managed_tag_value:
tags_info = {"Managed": "Not Managed"}
else:
tags_info = {"Managed": managed_tag_value}
print(tags_info)
managed_tag_value = tags['managed']
TypeError: 'NoneType' object is not subscriptable
{'name': '1111name', 'Managed': 'Not Managed'}
{'name': '2222name', 'Managed': 'notbillable'}
{'name': 'BastionRDP/MicrosoftMonitoringAgent', 'Managed': 'Not Managed'}
如何处理字典中填充了其他默认值(如NotAvailable等)的“无”值结果
编辑1:
tags_info = []
resource_list = client.resources.list()
for item in (resource_list):
tags = item.tags
managed_tag_value = tags['managed']
if not managed_tag_value:
tags_info = {"Managed": "Not Managed"}
else:
tags_info = {"Managed": managed_tag_value}
print(tags_info)
managed_tag_value = tags['managed']
TypeError: 'NoneType' object is not subscriptable
{'name': '1111name', 'Managed': 'Not Managed'}
{'name': '2222name', 'Managed': 'notbillable'}
{'name': 'BastionRDP/MicrosoftMonitoringAgent', 'Managed': 'Not Managed'}
myresources.list()的每次迭代如何输出项的示例数据如下
{'additional_properties': {}, 'id': '/subscriptions/xxxxxxxxx/resourceGroups/xxxxxxx/providers/Microsoft.Compute/snapshots/xxxxx', 'name': 'resourcename1', 'type': 'Microsoft.Compute/snapshots', 'location': 'southeastasia', 'tags': {'Name': 'xxxxxx', 'owner': 'xxxxxx', 'created date': '06022020', 'purpose': 'xxxx', 'Retention days ': 'NA'}, 'plan': None, 'properties': None, 'kind': None, 'managed_by': None, 'sku': xxxxxx, 'identity': None, 'created_time': None, 'changed_time': None, 'provisioning_state': None}
{'additional_properties': {}, 'id': '/subscriptions/xxxxxxxxxx/resourceGroups/xxxxxxx/providers/Microsoft.Compute/virtualMachines/xxxxx', 'name': 'resourcename2', 'type': 'Microsoft.Compute/virtualMachines', 'location': 'eastus', 'tags': {'Function': 'Fetch', 'Division': 'PH', 'managed_by': 'xxxxxx', 'managed': 'notbillable'}, 'plan': None, 'properties': None, 'kind': None, 'managed_by': None, 'sku': None, 'identity': None, 'created_time': None, 'changed_time': None, 'provisioning_state': None}
{'additional_properties': {}, 'id': '/subscriptions/xxxxxxxx/resourceGroups/xxxxxxx/providers/Microsoft.Compute/virtualMachines/xxxxx/extensions/MicrosoftMonitoringAgent', 'name': 'BastionRDP/MicrosoftMonitoringAgent', 'type': 'Microsoft.Compute/virtualMachines/extensions', 'location': 'eastus', 'tags': None, 'plan': None, 'properties': None, 'kind': None, 'managed_by': None, 'sku': None, 'identity': None, 'created_time': None, 'changed_time': None, 'provisioning_state': None}
我需要解决以下场景:
tags_info = []
resource_list = client.resources.list()
for item in (resource_list):
tags = item.tags
managed_tag_value = tags['managed']
if not managed_tag_value:
tags_info = {"Managed": "Not Managed"}
else:
tags_info = {"Managed": managed_tag_value}
print(tags_info)
managed_tag_value = tags['managed']
TypeError: 'NoneType' object is not subscriptable
{'name': '1111name', 'Managed': 'Not Managed'}
{'name': '2222name', 'Managed': 'notbillable'}
{'name': 'BastionRDP/MicrosoftMonitoringAgent', 'Managed': 'Not Managed'}
我需要找出每个字典的标签,同时,如果标签中有多个项目,我只需要从标签中获取“托管”
键
如果'managed'
键为null
,则将其值设置为'nottaged'
如果'tags'
键本身在上面的字典中不存在,那么它应该显示“tags key missing”
首先:tags[“managed”]
如果tags为none,则永远不会工作。这就像键入9.5[0]
。因为它没有索引,所以不能为它编制索引。
其次,如果他们键入“无”,则您想打印某些内容,请不要编辑代码并尝试以下操作:
try:
[insert code here]
except TypeError as error:
print("Not subscriptable")
当它抛出错误时,except语句将捕获它,但仅当它抛出TypeError时。这意味着,如果您将9.5而不是“无”,那么输出将是相同的。为什么不在赋值之前尝试检查键是否存在
tags_info = []
for item in client.resource_groups.list():
print(item.name)
tags = item.tags
if 'managed' in tags.keys(): #this will prevent the error. assume tags is a dictionary
managed_tag_value = tags['managed']
if not managed_tag_value:
tags_info = {"Managed": "Not Managed"}
else:
tags_info = {"Managed": managed_tag_value}
print(tags_info)
您甚至可以通过这样做进一步简化代码:
tags_info = []
for item in client.resource_groups.list():
print(item.name)
tags = item.tags
if 'managed' in tags.keys(): #this will prevent the error. assume tags is a dictionary
tags_info = {"Managed": tags['managed']}
else:
tags_info = {"Managed": "Not Managed"}
print(tags_info)
另外,您可以分享client.resource_组的内容吗。它看起来像一本字典。这可能更容易进一步简化
假设资源组是一个字典,并且密钥具有“托管”
。作为一个值,您可以执行以下操作
假设资源组是一个包含以下数据项的字典:
resource_groups = {
'abcd':'sample data 1',
'defg':'sample data 2',
'pqrs':'sample data 3',
'wxyz':'sample data 4'}
然后,您可以直接检查键'wxyz'
是否存在,如果存在,您可以对此采取措施。代码如下所示
tag_info = {}
if 'wxyz' in resource_groups.keys():
tags_info = {'Managed':resource_groups['wxyz']}
else:
tags_info = {'Managed':'Not Managed'}
print(tags_info)
基于样本数据的新答案
r = {
'1':{'additional_properties': {},
'id': '/subscriptions/1111xxxxx/rest of text',
'name': 'sxxxxxxx',
'type': 'Microsoft.Compute/snapshots',
'location': 'southeastasia',
'tags': {
'Name': '1111name',
'owner': 'xxxxxx',
'created date': '06022020',
'purpose': 'xxxx',
'Retention days ': 'NA'},
'plan': None,
'properties': None,
'kind': None,
'managed_by': None,
'sku': 'xxxxxx',
'identity': None,
'created_time': None,
'changed_time': None,
'provisioning_state': None},
'2':{'additional_properties': {},
'id': '/subscriptions/2222xxxxxxx/rest of text',
'name': '2222name',
'type': 'Microsoft.Compute/virtualMachines',
'location': 'eastus',
'tags': {
'Function': 'Fetch',
'Division': 'PH',
'managed_by': '222xxx',
'managed': 'notbillable'},
'plan': None,
'properties': None,
'kind': None,
'managed_by': None,
'sku': None,
'identity': None,
'created_time': None,
'changed_time': None,
'provisioning_state': None},
'3':{'additional_properties': {},
'id': '/subscriptions/3333xxxx/remaining text',
'name': 'BastionRDP/MicrosoftMonitoringAgent',
'type': 'Microsoft.Compute/virtualMachines/extensions',
'location': 'eastus',
'tags': None,
'plan': None,
'properties': None,
'kind': None,
'managed_by': None,
'sku': None,
'identity': None,
'created_time': None,
'changed_time': None,
'provisioning_state': None}
}
tags_info = {}
for k,v in r.items():
#first check if name is a key
if 'name' in v.keys():
#then store name as a value
tags_info['name'] = v['name']
else:
#store 'No Name' as a value
tags_info['name'] = 'No Name'
#check if 'tags' is a key and
#check if 'tags' is a dictionary
if 'tags' in v.keys() and v['tags']:
#check if 'managed' is a key inside 'tags' dictionary
if 'managed' in v['tags'].keys():
#then we got what we want
tags_info['Managed'] = v['tags']['managed']
else:
#tags does not have 'managed' as a key
tags_info['Managed'] = 'Not Managed'
print (tags_info)
else:
#tags is not a dictionary so it is not managed
tags_info['Managed'] = 'Not Managed'
print (tags_info)
上述代码的输出为:
tags_info = []
resource_list = client.resources.list()
for item in (resource_list):
tags = item.tags
managed_tag_value = tags['managed']
if not managed_tag_value:
tags_info = {"Managed": "Not Managed"}
else:
tags_info = {"Managed": managed_tag_value}
print(tags_info)
managed_tag_value = tags['managed']
TypeError: 'NoneType' object is not subscriptable
{'name': '1111name', 'Managed': 'Not Managed'}
{'name': '2222name', 'Managed': 'notbillable'}
{'name': 'BastionRDP/MicrosoftMonitoringAgent', 'Managed': 'Not Managed'}
“标签”有时是没有的吗?如果是,你不能做标记['managed']。是的,有时任何资源组都没有标记,所以我需要将这些标记打印为“notmanaged”managed\u tag\u value=tags['managed'],如果其他标记没有,对不起,无法获得它..请正确格式化它…我不知道你还想要什么,我为你写了一行代码。你能给我举一个内容资源组的例子吗。我想你可能不需要标签和其他很多东西。我制作了一些示例数据来向你展示我们如何实现简化的代码。看看这是否对你有帮助。看看我的新答案。这是一个很长的问题。请再次检查。如果您想让我也打印第三项的回复,请告诉我。为第三个也添加了代码。看看这是否有用。如果是,请向上投票我的回答并将代码标记为已回答。很高兴在此基础上进一步阐述。为此编写代码很有趣。