title: Appending items at the end of each row in a CSV file
body: <p>I'm trying to write a code that writes a CSV file that has, 1 header row, and 20 other rows, each one containing the following: 1 random name, 8 grades [randomly generated], and 1 final grade averaged from the other 8 random grades. I've gotten so far as to read the file and generate the average final grades and append it to an empty list so i can re-enter it to the file, but I'm having trouble writing/appending them back into the file.</p>
  
  <p>I see that there is no <code>x.writecolumns()</code> and I'm having a lot of trouble trying to accomplish this step. Here is what i have so far. Thanks in advance for anyone that drops by to give advice on this.</p>
  
  <pre><code>import random
  import csv
  
  fname = ['Red', 'Jose', 'Mikasa', 'Sakura', 'Naruto', 'Momoka', 'Zero', 'Ryu', 'Ken', 'Geralt', 'Greg', 'Mika', 'Momo',
       'Xenovia', 'John', 'Gary', 'Lance', 'Eren', 'Duke', 'Claire']
  lname = ['Akerman', 'Haruno', 'Johnson', 'Rivia', 'Valentine', 'Redfield', 'Kennedy', 'Juarez', 'Jameson', 'Williams',
       'Miller', 'Davis', 'Thompson', 'Garcia', 'Remington', 'Hayabusa', 'Strife', 'Armstrong', 'Kojima', 'Chambers']
  
  avg_list = list() 
  #^List where im appending all the average grades for all students
  
  def ranint():
      value = random.randint(60, 100)
      return value
  #^random value generator 
  
  def ranname():
      first = random.choice(fname)
      last = random.choice(lname)
      fname.remove(first)
      lname.remove(last)
      return ("; ".join([first, last]))
  #^picks a random first name and random last name
  
  #CSV writer that has 1 header row and 20 other rows, each consisting of 1 name, 8 random grades, and 1 final grade averaged from the other 8
  with open('grades2.csv', 'w', newline='') as fp:
  a = csv.writer(fp, delimiter=',')
  data = [['Name', 'If Else', 'Loops', 'Functions', 'File IO', 'Conditionals', 'Repetition', 'Python Functions',
           'Input Output', 'Grade'],
          [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
          [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
          [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
          [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
          [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
          [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
          [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
          [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
          [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
          [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
          [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
          [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
          [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
          [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
          [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
          [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
          [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
          [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
          [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()],
          [ranname(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint(), ranint()]]
  a.writerows(data)
  fp.close()
  
  #Reads the file, sums all 8 grades and gets the average. Does this for all students and then appends the results to a empty list
  with open('grades2.csv', 'r+', newline='') as fh:
  z = csv.reader(fh)
  next(z)   #skip the header
  for row in z:
      integers = [int(i) for i in row[1:9]]
      avg_integers = sum(integers) / len(integers)
      avg_list.append(avg_integers)
      print(avg_integers) #This is just here to check with the final CSV file
  fh.close()
  </code></pre>