如何获取复杂字典的整数值
How to get an integer value to a complex dictionary
这里abc的值是一个整数,total是一个包含另一个dictionaries.But的字典,当我运行这一行,
total[div]['total_transport_allowance'] = abc
显示此错误:"KeyError: None"
请帮忙
我想从这个语句中得到 abc 的值到键 'total_transport_allowance'
def print_excel_report(self,cr,uid,ids,data,context=None):
result = self._get_lines(cr,uid,ids,data)
filename= 'PayrollRegister.xls'
workbook= xlwt.Workbook(encoding="UTF-8")
sheet= workbook.add_sheet('Payroll Register',cell_overwrite_ok=True)
style = xlwt.easyxf('font:height 400, bold True, name Arial; align: horiz center, vert center;borders: top medium,right medium,bottom medium,left medium')
a = range(1,10)
row = 1
col =0
header = ['Division','Basic','Transport Allowance']
style2 = xlwt.easyxf('font: bold 1')
total = {}
for index,data in enumerate(result):
div = data.get('Division',False)
abc = data.get('transport_allowance',False) or 0
if div:
if div in total:
total[div]['total_basic'] = total[div]['total_basic'] + data.get('basic',0)
total[div]['total_transport_allowance'] = total[div]['total_transport_allowance'] + abc
else:
total[div] = {}
total[div]['total_basic'] = data.get('basic',0)
total[div]['total_transport_allowance'] = abc
else:
if 'Undefined' in total:
total['Undefined']['total_basic'] = total['Undefined']['total_basic'] + data.get('basic',0)
total[div]['total_transport_allowance'] = total[div]['total_transport_allowance'] + abc
else:
total['Undefined'] = {}
total['Undefined']['total_basic'] = data.get('basic',0)
total[div]['total_transport_allowance'] = abc
.......
.....
尝试从所有反对票中挽救一些东西:
你有一个 if 语句来检查 div
的真实性。在错误的情况下,您然后使用 div 访问总数。
即你有:
if div:
# stuff
else:
# stuff
total[div]['total_transport_allowance'] = abc
由于 total 没有密钥 div,这不起作用。
你需要 total[div] = {}
您收到此错误是因为 div
是 None
:
if div:
....
....
else:
...
...
# div here is None or False ..
# you cannot use it as key!!
total[div]['total_transport_allowance'] = abc
您需要先在此处定义密钥。所以在哪里
当 div 为 None.
时,您想放置 abc
编辑
我不知道 div 不在 data.get('Division',False)
中时的值应该是多少:
div = data.get('Division', False)
...
...
if div:
.....
else:
# assing a value to div
div = 'NEW_VALUE'
# add dictionary there
total[div] = {}
if 'Undefined' in total:
....
...
...
total[div]['total_transport_allowance'] = {}
这有效..
我在
中这样做过
total['Undefined']['total_transport_allowance'] = {}
还有..
感谢大家的帮助
KeyError is None 意味着没有定义字典。 div是一个字典应该定义。
这里abc的值是一个整数,total是一个包含另一个dictionaries.But的字典,当我运行这一行,
total[div]['total_transport_allowance'] = abc
显示此错误:"KeyError: None"
请帮忙
我想从这个语句中得到 abc 的值到键 'total_transport_allowance'
def print_excel_report(self,cr,uid,ids,data,context=None):
result = self._get_lines(cr,uid,ids,data)
filename= 'PayrollRegister.xls'
workbook= xlwt.Workbook(encoding="UTF-8")
sheet= workbook.add_sheet('Payroll Register',cell_overwrite_ok=True)
style = xlwt.easyxf('font:height 400, bold True, name Arial; align: horiz center, vert center;borders: top medium,right medium,bottom medium,left medium')
a = range(1,10)
row = 1
col =0
header = ['Division','Basic','Transport Allowance']
style2 = xlwt.easyxf('font: bold 1')
total = {}
for index,data in enumerate(result):
div = data.get('Division',False)
abc = data.get('transport_allowance',False) or 0
if div:
if div in total:
total[div]['total_basic'] = total[div]['total_basic'] + data.get('basic',0)
total[div]['total_transport_allowance'] = total[div]['total_transport_allowance'] + abc
else:
total[div] = {}
total[div]['total_basic'] = data.get('basic',0)
total[div]['total_transport_allowance'] = abc
else:
if 'Undefined' in total:
total['Undefined']['total_basic'] = total['Undefined']['total_basic'] + data.get('basic',0)
total[div]['total_transport_allowance'] = total[div]['total_transport_allowance'] + abc
else:
total['Undefined'] = {}
total['Undefined']['total_basic'] = data.get('basic',0)
total[div]['total_transport_allowance'] = abc
.......
.....
尝试从所有反对票中挽救一些东西:
你有一个 if 语句来检查 div
的真实性。在错误的情况下,您然后使用 div 访问总数。
即你有:
if div:
# stuff
else:
# stuff
total[div]['total_transport_allowance'] = abc
由于 total 没有密钥 div,这不起作用。
你需要 total[div] = {}
您收到此错误是因为 div
是 None
:
if div:
....
....
else:
...
...
# div here is None or False ..
# you cannot use it as key!!
total[div]['total_transport_allowance'] = abc
您需要先在此处定义密钥。所以在哪里 当 div 为 None.
时,您想放置 abc编辑
我不知道 div 不在 data.get('Division',False)
中时的值应该是多少:
div = data.get('Division', False)
...
...
if div:
.....
else:
# assing a value to div
div = 'NEW_VALUE'
# add dictionary there
total[div] = {}
if 'Undefined' in total:
....
...
...
total[div]['total_transport_allowance'] = {}
这有效.. 我在
中这样做过total['Undefined']['total_transport_allowance'] = {}
还有.. 感谢大家的帮助
KeyError is None 意味着没有定义字典。 div是一个字典应该定义。