The continue
statement is working as you expect. The 3rd line - "No Data" is coming from a change in d
to Research and development
. I've added a few print statements to demonstrate:
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:
print ''
print d
for i in raw_financial_data:
try:
if i[1] == 'TTM' and ttm_count == 0:
print '1st append', i
financial_data.append(i)
ttm_count = 1
continue
except IndexError:
print 'IndexError'
continue
if i[0] == d:
print '2nd append', i
financial_data.append(i)
elif d not in rfd_header:
rfd_header.append(d)
print '3nd append', 'No Data'
financial_data.append(['No Data', 'N/A', 'N/A',
'N/A', 'N/A', 'N/A','N/A'])
else:
print 'no append'
return financial_data
if __name__ == "__main__":
for row in get_data():
print row
And here's the output:
Gross profit
1st append ['Fiscal year ends in December. USD in millions except per share data.', 'TTM', '2012-12', '2011-12', '2010-12', '2009-12', '2008-12']
2nd append ['Gross profit', '125390', '146216', '179627', '120923', '98817', '188549']
IndexError
no append
no append
Research and development
3nd append No Data
no append
IndexError
no append
no append
Total costs and expenses
no append
no append
IndexError
2nd append ['Total costs and expenses', '64695', '67490', '106370', '67964', '64040', '106799']
no append
Total operating expenses
3nd append No Data
no append
IndexError
no append
no append
Operating income
3nd append No Data
no append
IndexError
no append
no append
Income before income taxes
no append
no append
IndexError
no append
2nd append ['Income before income taxes', '60695', '78726', '73257', '52959', '34777', '81750']
['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']