鉴于这些数据
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
我想跳过以Organs和Genes开头的前两行。 我试图做的是:
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 :(得分:3)
但为什么失败了?
因为您检查了["Genes", "Organ"]
是否在行中(它不是,因为您检查单个元素的列表是否存在双元素子列表),而您可能想要以另一种方式构建它:
# row[0] is a single element ...
# .. that might be in ("Genes", "Organ")
if row[0] in ("Genes", "Organ"):
# ...
答案 1 :(得分:1)
if "Genes" in row or "Organ" in row
应该做的伎俩!
你的错误是假设table + table操作符的行为与object + table相同。请注意这一点。