2014-03-12 31 views
1

鑑於這些數據跳繩線間歇性地在Python

Probes FOO BAR 
Organ BM LV 
Genes Gene1 Gene2 
1452463_x_at 306.564  185.705 
1439374_x_at 393.742  330.495 
1426392_a_at 269.850  209.931 

我想跳過打印頭兩行與器官和基因開始。 我試圖做的是:

with open('my_data.txt','r') as tsvfile 
    tabreader = csv.reader(tsvfile,delimiter = '\t') 

    for row in tabreader: 
     if ["Genes","Organ"] in row: 
      continue 
     print row 

但爲什麼失敗?

最終所需的輸出是這樣的:

Probes FOO BAR 
1452463_x_at 306.564  185.705 
1439374_x_at 393.742  330.495 
1426392_a_at 269.850  209.931 
+0

在Python,它更容易只是negaate用'not'函數的聲明。例如,你可能會考慮使用表達式:'如果行[0]不是(「基因」,「器官」):打印row' – ssm

回答

3

但爲什麼會失敗?

因爲你檢查,["Genes", "Organ"]是否行(它不是,因爲你檢查單元素的列表兩個元素的子表的存在),而你可能想幀它的其他方式:

# row[0] is a single element ... 
# .. that might be in ("Genes", "Organ") 
if row[0] in ("Genes", "Organ"): 
    # ... 
1
if "Genes" in row or "Organ" in row 

應該做的伎倆!

你的錯誤是假設表+表運算符的行爲相同的對象+表。小心這樣的。