Python 在try:except:中继续似乎不会跳过代码块

Python 在try:except:中继续似乎不会跳过代码块,python,exception,continue,Python,Exception,Continue,我在使用异常处理从原始列表创建自定义格式的列表时遇到困难。我的代码如下:对不起,代码墙,大部分代码只是定义了所涉及的列表: def get_data(): header_list = ['Gross profit', 'Research and development', 'Total costs and expenses', 'Total operating expenses', 'Operating inc

我在使用异常处理从原始列表创建自定义格式的列表时遇到困难。我的代码如下:对不起,代码墙,大部分代码只是定义了所涉及的列表:

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
我得到的结果是:

['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']
我想做的是从财务数据中省略上面输出的第3行。其余的“无数据”行与预期的一样,但我不确定为什么“除索引器:继续”不跳到原始财务数据中的下一个I,而不附加“无数据”行,因为应为标题列表中的项目[“成本和费用”]生成索引器

如果有更好的方法来实现这个结果,我愿意接受,但我想理解,当我认为带有financial_Data.append的整个块被continue语句跳过时,为什么在代码中追加“No Data”行

continue语句正在按预期工作。第三行-没有数据来自d到研发的变化。我添加了一些打印语句来演示:

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
以下是输出:

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']

请发布一个最低限度的工作示例:-弗雷德里克·皮尔:完成。现在不用滚动就可以看到相关的位。Peter,我在原地盯着这段代码看得太久了,对输出应该是什么抱有成见。谢谢你帮助我看到我自己的代码实际上在做什么,而不是我认为它应该做什么。@dman没问题,总是发生。您应该使用pdb(python调试器)对代码进行一次运行,您会立即发现问题。虽然我认为有一点是值得一提的,那就是不要直接求助于调试器来理解代码。