Skip to content

Instantly share code, notes, and snippets.

@Nyahua
Created March 29, 2017 15:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nyahua/7db3056ce2ba6df9ec84dd362eabe686 to your computer and use it in GitHub Desktop.
Save Nyahua/7db3056ce2ba6df9ec84dd362eabe686 to your computer and use it in GitHub Desktop.
append new row to old csv file python

using the csv module from the standard library and the open method to avoid leaving the file open.

The key point is using 'a' for appending when you open the file.

import csv   
fields=['first','second','third']
with open(r'name', 'a') as f:
    writer = csv.writer(f)
    writer.writerow(fields)
    

You may experience superfluous new lines in Windows. You can try to avoid them using 'ab' instead of 'a'. CSV is a binary format, believe it or not.

http://stackoverflow.com/questions/2363731/append-new-row-to-old-csv-file-python http://stackoverflow.com/questions/4249185/using-python-to-append-csv-files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment