title: Querying and writing keywords to a new column
body: <p>I have a script I wrote with the help of some online resources but it isn't quite doing what I need it to.  I am taking an existing csv file, querying 4 textfields within that file for a group of keywords and than writing the results to a new CSV file.  Right now the script only looks at any given textfield and than if it finds any of the keywords within the text file, it writes each keyword to a separate column for that record.  I need to be able to do 2 things with the script.</p>
  
  <p>1) Query more than one textfield.  So instead of just 'essay1', I need to query 'essay1','essay2','essay3','essay4' for all those keywords.</p>
  
  <p>2) If the script finds any of those keywords, it should write whatever keyword it finds into one column versus individual columns for that particular record.  </p>
  
  <pre><code>import csv
  import time
  
  #Read data from word enrichment
  f = open('word_enrichment_approved.txt', 'r')
  allKeywords = f.read().lower().split("\n")
  f.close()
  
  #Read in raw data from csv file
  #Import the 'essay' column from the CSV file
  allTexts = []
  fullRow = []
  with open('Join_train_and_resources_edited.csv') as csvfile:
      reader = csv.DictReader(csvfile)
      for row in reader:
          #the full row for each entry, which will be used to recreate the improved CSV file 
          fullRow.append((row['record_id'],row['teacher_id'],row['teacher_prefix'],row['school_state'],row['project_submitted_datetime'],
                          row['project_grade_category'], row['project_subject_categories'], row['project_subject_subcategories'],
                          row['project_title'],row['project_essay_1'],row['project_essay_2'],row['project_essay_3'],row['project_essay_4'],
                          row['project_resource_summary'],row['teacher_number_of_previously_posted_projects'],row['project_is_approved'],
                          row['id-1'],row['quantity'],row['price']))
  
          #the column we want to parse for our keywords (change to mactch actual name of the column)
          row = row['project_essay_1'].lower()
          allTexts.append(row)
  #NEW! a flag used to keep track of which row is being printed to the CSV file   
  counter = 0
  #NEW! use the current date and time to create a unique output filename
  timestr = time.strftime("%Y-%m-%d-(%H-%M-%S)")
  filename = 'output-' + str(timestr) + '.csv'
  
  #NEW! Open the new output CSV file to append ('a') rows one at a time.
  with open(filename, 'a') as csvfile:
  
      #NEW! define the column headers and write them to the new file
      fieldnames = ['id', 'teacher_id','teacher_prefix    ','school_state','project_submitted_datetime','project_grade_category',
                    'project_subject_categories','project_subject_subcategories','project_title','project_essay_1','project_essay_2',
                    'project_essay_3','project_essay_4','project_resource_summary','teacher_number_of_previously_posted_projects',
                    'project_is_approved','id-1','quantity','price']
      writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
      writer.writeheader()
  
      #NEW! define the output for each row and then print to the output csv file
      writer = csv.writer(csvfile)
  
      for entry in allTexts:
  
          matches = 0
          storedMatches = []
  
          #for each entry:
          allWords = entry.split(' ')
          for words in allWords:
  
              #remove punctuation that will interfere with matching
              words = words.replace(',', '')
              words = words.replace('.', '')
              words = words.replace(';', '')
  
              #if a keyword match is found, store the result.
              if words in allKeywords:
                  if words in storedMatches:
                      continue
                  else:
                      storedMatches.append(words)
                  matches += 1
  
          #CHANGED! send any matches to a new row of the csv file.
          if matches == 0:
              newRow = fullRow[counter]
          else:
              matchTuple = tuple(storedMatches)
              newRow = fullRow[counter] + matchTuple
  
          #NEW! write the result of each row to the csv file
          writer.writerows([newRow])
          counter += 1
  </code></pre>