有如下的串列
[{'id': 1, 'name': 'pool', 'title': None, 'hostId': None, 'hostName': None, 'clusterId': None, 'clusterName': None, 'hpId': None, 'hpName': None, 'status': None}, {'id': 20, 'name': 'test', 'title': None, 'hostId': None, 'hostName': None, 'clusterId': None, 'clusterName': None, 'hpId': None, 'hpName': None, 'status': None}, None, {'id': None, 'name': None, 'title': None, 'hostId': None, 'hostName': None, 'clusterId': None, 'clusterName': None, 'hpId': None, 'hpName': None, 'status': 'false'}, None, None]
部分字典中name的值為none,在遍歷獲取name的值的時候,會有如下錯誤
for i in datas:
if i["name"]==pool_name:\\判斷name的值與某個入參一致
print (i["id"])
報錯如下
Traceback (most recent call last):
File "D:/interface-test-python/yunziyuan/pool.py", line 32, in <module>
get_poolId("pool")
File "D:/interface-test-python/yunziyuan/pool.py", line 30, in get_poolId
if i["name"]==pool_name:
TypeError: 'NoneType' object is not subscriptable
應該是其中的None值的問題,這個錯誤怎么處理比較合適?
uj5u.com熱心網友回復:
for i in datas:改為
for i in filter(lambda x:x is not None , datas):
uj5u.com熱心網友回復:
datas=[{'id': 1, 'name': 'pool', 'title': None, 'hostId': None, 'hostName': None, 'clusterId': None, 'clusterName': None, 'hpId': None, 'hpName': None, 'status': None}, {'id': 20, 'name': 'test', 'title': None, 'hostId': None, 'hostName': None, 'clusterId': None, 'clusterName': None, 'hpId': None, 'hpName': None, 'status': None}, None, {'id': None, 'name': None, 'title': None, 'hostId': None, 'hostName': None, 'clusterId': None, 'clusterName': None, 'hpId': None, 'hpName': None, 'status': 'false'}, None, None]
for i in datas:
if i!=None:
if i['name']=='pool':
print(i['id'])
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/55666.html
