Here the value of abc is an integer and total is a dictionary which contains another dictionaries.But when I run this line,
total[div]['total_transport_allowance'] = abc
This error is shown : "KeyError: None"
Please Help
What I want from this statement is to get the abc's value to the key '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
.......
.....
You are getting this error because div
is None
:
if div:
....
....
else:
...
...
# div here is None or False ..
# you cannot use it as key!!
total[div]['total_transport_allowance'] = abc
You need to define the key here first. so where you want to put the abc when div is None.
EDIT
I don't know what the value of div should when it's not in 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:
....
...
...