Python习惯用法跳过匹配条件的第一行

时间:2014-03-12 01:36:55

标签: python parsing

我有一个制表符分隔的数据,如下所示:

Probes  FOO BAR
1452463_x_at    306.564     185.705 
1439374_x_at    393.742     330.495 
1426392_a_at    269.850     209.931 
1433432_x_at    636.145     487.012 
1415687_a_at    231.547     175.008 
1424736_at  248.926     189.500 
1435324_x_at    244.901     225.842 
1438688_at  180.511     187.407 
1426906_at  206.694     218.913 

我想要解决的是上述数据。但首先必须跳过以Probes开头的行。但为什么这条线失败了?什么是最常见的Pythonesque方法来处理这些问题?

import sys
import csv
import re 

with open('Z100_data.txt','r') as tsvfile:
    tabreader = csv.reader(tsvfile,delimiter='\t')
    for row in tabreader:
        if re.match("^Probes",row):
            # He we try to skip the first line.
            continue
        else:

           print ', '.join(row)

2 个答案:

答案 0 :(得分:2)

处理此问题的pythonic方法是next()

with open('Z100_data.txt','r') as tsvfile
    tabreader = csv.reader(tsvfile,delimiter = '\t')
    next(tabreader) # skips the first line
    for row in tabreader:
        print ', '.join(row)

答案 1 :(得分:1)

尝试:

if re.match("^Probes",' '.join(row)):

或:

if "Probes" in row:

或:

if "Probes" == row[0].strip():

csv.reader返回由分隔符分割的行中的元组列表。您尝试使用re o list / tuple,而它仅适用于字符串。