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 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']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.