我想從python中的嵌套字典中獲取一個值。問題是,當嵌套字典的父母不可用,那麼我希望它返回一個無或零,但我想我使用get的問題已經返回一個Nonetype,因此我得到一個錯誤,當我使用:用get命令嵌套字典
pl_map.get(item)["ref_rate"]
現在的代碼工作正常時,產品的字典可用,但它提出了一個TypeError作爲NoneType對象unsubscriptable。
任何一個可以請讓我知道如何解決這個問題,我已經粘貼下面的代碼的某些部分。
基本上pl_map是從下面的函數看到的嵌套字典。對於我可能做出的任何遺漏,我表示歉意。
for item in sorted(iwb_map):
for wh in sorted(iwb_map[item]):
#webnotes.msgprint(pl_map.get(item,0))
qty_dict = iwb_map[item][wh]
data.append([item,item_map[item]["description"], wh,
qty_dict.bal_qty,pl_map.get(item)["ref_rate"],0,0,item_map[item]["base_material"],
item_map[item]["quality"], item_map[item]["tool_type"],
item_map[item]["height_dia"], item_map[item]["width"],
item_map[item]["length"], item_map[item]["d1"],
item_map[item]["l1"], item_map[item]["is_rm"],
item_map[item]["brand"]
])
def get_pl_map(filters):
if filters.get("pl"):
conditions = " and price_list_name = '%s'" % filters["pl"]
else:
webnotes.msgprint("Please select a Price List for Valuation Purposes", raise_exception=1)
pl_map_int = webnotes.conn.sql ("""SELECT it.name, p.price_list_name, p.ref_rate
FROM `tabItem` it, `tabItem Price` p
WHERE p.parent = it.name %s
ORDER BY it.name""" % conditions, as_dict=1)
pl_map={}
for d in pl_map_int:
pl_map.setdefault(d.name,d)
#webnotes.msgprint(pl_map)
return pl_map
這只是保存 我的生活。 – 2014-01-31 22:35:39