2014-03-12 41 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 

应该做的伎俩!

你的错误是假设表+表运算符的行为相同的对象+表。小心这样的。