I am having difficulty using exception handling to create a custom formatted list of lists from a raw list of lists. My code is as follows (sorry for the wall of code, most of it is just defining the lists involved):
def get_data():
header_list = ['Gross profit', 'Research and development',
'Total costs and expenses', 'Total operating expenses',
'Operating income', 'Income before income taxes']
raw_financial_data = [['Fiscal year ends in December. USD in millions'
' except per share data.', 'TTM', '2012-12',
'2011-12', '2010-12', '2009-12', '2008-12'],
['Gross profit', '125390', '146216', '179627',
'120923', '98817', '188549'], ['Costs and expenses'],
['Total costs and expenses', '64695', '67490',
'106370', '67964', '64040', '106799'],
['Income before income taxes', '60695', '78726',
'73257', '52959', '34777', '81750']]
financial_data = []
rfd_header = [h[0] for h in raw_financial_data]
ttm_count = 0
for d in header_list:
for i in raw_financial_data:
try:
if i[1] == 'TTM' and ttm_count == 0:
financial_data.append(i)
ttm_count = 1
continue
except IndexError:
continue
if i[0] == d:
financial_data.append(i)
elif d not in rfd_header:
rfd_header.append(d)
financial_data.append(['No Data', 'N/A', 'N/A',
'N/A', 'N/A', 'N/A','N/A'])
return financial_data
if __name__ == "__main__":
for row in get_data():
print row
The output that I am getting is:
['Fiscal year ends in December. USD in millions except per share data.', 'TTM', '2012-12', '2011-12', '2010-12', '2009-12', '2008-12']
['Gross profit', '125390', '146216', '179627', '120923', '98817', '188549']
['No Data', 'N/A', 'N/A', 'N/A', 'N/A', 'N/A', 'N/A']
['Total costs and expenses', '64695', '67490', '106370', '67964', '64040', '106799']
['No Data', 'N/A', 'N/A', 'N/A', 'N/A', 'N/A', 'N/A']
['No Data', 'N/A', 'N/A', 'N/A', 'N/A', 'N/A', 'N/A']
['Income before income taxes', '60695', '78726', '73257', '52959', '34777', '81750']
What I want to happen is for line 3 of the output above to be omitted from financial_data
. The rest of the 'No Data' rows are as expected, but I am not sure why the except IndexError: continue
does not skip to the next i
in raw_financial_data
without appending a 'No Data' row, since an IndexError
should be raised for the item ['Costs and expenses']
in header_list
.
I am open to a better methodology to achieve this result if there is one, but I would like to understand why 'No Data' rows are appended in this code when I thought that the whole block with the financial_data.append
was being skipped with a continue
statement.
The
continue
statement is working as you expect. The 3rd line - "No Data" is coming from a change ind
toResearch and development
. I've added a few print statements to demonstrate:And here's the output: