2013-12-08 36 views
0

我很難使用異常處理從列表的原始列表中創建列表的自定義格式列表。我的代碼如下(爲代碼在牆上對不起,大部分是剛剛確定所涉及的列表):繼續嘗試:除了:似乎不跳過代碼塊

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行將從financial_data中省略。 '無數據'行的其餘部分是預期的,但我不知道爲什麼except IndexError: continue不跳過iraw_financial_data沒有追加'無數據'行,因爲IndexError應該爲項目['Costs and expenses']header_list

如果存在一個更好的方法來實現這個結果,但我想知道爲什麼'No Data'行被附加在這段代碼中,當我認爲與financial_data.append的整個塊被跳過與continue聲明。

+2

請發表** **最低工作示例:-) –

+0

弗雷德裏克皮赫爾:完成。相關位可以在不滾動的情況下看到。 – dman

回答

0

continue聲明正如您所期望的那樣工作。第三行 - 「無數據」來自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'] 
+0

彼得,我一直盯着這個代碼原地太久,對輸出應該是什麼先入爲主的觀念。謝謝你幫我看看我自己的代碼實際上是在做什麼,而不是我認爲應該做的事情。 – dman

+0

@dman沒有問題,一直髮生。你應該用'pdb'(python調試器)通過代碼運行,你會立即發現問題。儘管我認爲有些東西不能直接使用調試器來理解你的代碼。 –