title: Inline CSV File Editing with Python
body: <p>Can I modify a CSV file inline using Python's CSV library, or similar technique?</p>
  
  <p>Current I am processing a file and updating the first column (a name field) to change the formatting.  A simplified version of my code looks like this:</p>
  
  <pre><code>with open('tmpEmployeeDatabase-out.csv', 'w') as csvOutput:
      writer = csv.writer(csvOutput, delimiter=',', quotechar='"')
  
      with open('tmpEmployeeDatabase.csv', 'r') as csvFile:
          reader = csv.reader(csvFile, delimiter=',', quotechar='"')
  
          for row in reader:
              row[0] = row[0].title()
              writer.writerow(row)
  </code></pre>
  
  <p>The philosophy works, but I am curious if I can do an inline edit so that I'm not duplicating the file.</p>
  
  <p>I've tried the follow, but this appends the new records to the end of the file instead of replacing them.</p>
  
  <pre><code>with open('tmpEmployeeDatabase.csv', 'r+') as csvFile:
      reader = csv.reader(csvFile, delimiter=',', quotechar='"')
      writer = csv.writer(csvFile, delimiter=',', quotechar='"')
  
      for row in reader:
          row[1] = row[1].title()
          writer.writerow(row)
  </code></pre>