继续尝试:除了:似乎没有跳过代码块

Posted

技术标签:

【中文标题】继续尝试:除了:似乎没有跳过代码块【英文标题】:Continue in try: except: seems not to skip code block 【发布时间】:2013-12-08 20:52:38 【问题描述】:

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

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

我希望从financial_data 中省略上面输出的第 3 行。其余的“无数据”行与预期的一样,但我不确定为什么 except IndexError: continue 在不附加“无数据”行的情况下不会跳到 raw_financial_data 中的下一个 i,因为 IndexError应该为 header_list 中的项目 ['Costs and expenses'] 提出。

如果有一个更好的方法来实现此结果,我持开放态度,但我想了解为什么当我认为带有 financial_data.append 的整个块正在被添加时,为什么在此代码中附加了“无数据”行使用continue 语句跳过。

【问题讨论】:

请发布一个最低工作示例:-) 弗雷德里克·皮尔:完成。现在无需滚动即可看到相关位。 【参考方案1】:

continue 语句按预期工作。第 3 行 - “无数据”来自 dResearch and development 的更改。我添加了一些打印语句来演示:

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

【讨论】:

彼得,我一直在原地盯着这段代码太久,对输出应该是什么有先入为主的观念。感谢您帮助我了解我自己的代码实际上在做什么,而不是我认为它应该做什么。 @dman 没问题,一直在发生。您应该使用pdb(python 调试器)运行代码,您会立即发现问题。尽管我认为不直接使用调试器来理解您的代码也是有道理的。

以上是关于继续尝试:除了:似乎没有跳过代码块的主要内容,如果未能解决你的问题,请参考以下文章

为啥func调用中的代码块没有被执行?

如果找不到文件,如何跳过代码块?

ATA 磁盘驱动程序“跳过”内存块

python条件判断

Vue-if 条件不起作用。 if-Else 块似乎被跳过

在 Kotlin 中继续运行块