title: Appending a CSV file with a new column in python
body: <p>I am trying to create a clean csv file by merging some of variables together from an old file and appending them to a new csv file.</p>
  
  <p>I have no problem running the data the first time. I get the output I want but whenever I try to append the data with a new variable (i.e. new column) it appends the variable to the bottom and the output is wonky.  </p>
  
  <p>I have basically been running the same code for each variable, except changing the 
  groupvariables variable to my desired variables and then using the f2= open('outputfile.csv', "ab") &lt;--- but with an ab for amend.  Any help would be appreciated</p>
  
  <pre><code>groupvariables=['x','y']
  
  f2  = open('outputfile.csv', "wb")
  writer = csv.writer(f2, delimiter=",")
  writer.writerow(("ID","Diagnosis"))
  
  for line in csv_f:
      line = line.rstrip('\n')
      columns  = line.split(",")
      tempname = columns[0]
      tempindvar = columns[1:]
  
  templist = []
  
  for j in groupvariables:
      tempvar=tempindvar[headers.index(j)]
      if tempvar != ".":
          templist.append(tempvar)
  
  newList = list(set(templist))
  
  if len(newList) &gt; 1:
      output = 'nomatch'
  elif len(newList) == 0:
      output = "."
  else:
      output = newList[0]
  
  tempoutrow = (tempname,output)
  writer.writerow(tempoutrow)
  </code></pre>
  
  <p>f2.close()</p>