# ChildPhon pattern-finder ''' This program opens a ChildPhon CSV file, iterates over it, and gives matches for a given regular expression in a given column. ''' # for regular expressions and CSVs import re, csv # ChildPhon file to read from dataFile = "trevor.csv" # new file to create outputFile = "trevor-out.csv" # column to look at columnNumber = 6 # pattern to search for pattern = "bl" inFH = open(dataFile) outFH = open(outputFile,"w") # some CSV-specific things to make our lives easier reader = csv.reader(inFH, delimiter='\t', quotechar='\"') writer = csv.writer(outFH, delimiter='\t', quotechar='\"') # the first line is just the column labels header = reader.next() writer.writerow(header) # the iterable items in a CSV are the rows broken into lists for row in reader: # if pattern found... if(re.search(pattern, row[columnNumber])): # output record line writer.writerow(row) # not strictly necessary, good for larger programs inFH.close() outFH.close()