298

I am trying to add a new row to my old CSV file. Basically, it gets updated each time I run the Python script.

Right now I am storing the old CSV rows values in a list and then deleting the CSV file and creating it again with the new list value.

I wanted to know are there any better ways of doing this.

8 Answers 8

342
with open('document.csv','a') as fd:
    fd.write(myCsvRow)

Opening a file with the 'a' parameter allows you to append to the end of the file instead of simply overwriting the existing content. Try that.

3
  • 4
    I tried fp = open(csv_filepathwithname, 'wa') writer = csv.writer(fp) somelist = [ 3,56,3,6,56] writer.writerow((somelist)) but only the last row get append in the file.
    – laspal
    Mar 2, 2010 at 14:49
  • 3
    The method assumes that the items being appended are comma separated, which may not always be the case. Then the write method will not maintain the csv delimiter. The answer below is more robust in that sense.
    – sheth7
    Jan 30, 2020 at 23:22
  • Please provide myCsvRow type Nov 8, 2022 at 16:13
247

I prefer this solution using the csv module from the standard library and the with statement 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)

If you are using Python 2.7 you may experience superfluous new lines in Windows. You can try to avoid them using 'ab' instead of 'a' this will, however, cause you TypeError: a bytes-like object is required, not 'str' in python and CSV in Python 3.6. Adding the newline='', as Natacha suggests, will cause you a backward incompatibility between Python 2 and 3.

3
  • 3
    Annoying issue that can arise: If the csv file doesn't end with a newline, this will extend the final row instead of adding a new one. Feb 24, 2021 at 19:57
  • 2
    This is the better answer to the question, and should be marked as correct answer. The csv package ensures formatting correctly, in fact I can't think of any advantage of using plain text over csv.
    – Yost777
    Mar 19, 2021 at 12:56
  • 6
    For me this code was adding superfluous new lines so I added a newline='' to this row: with open(r'name', 'a', newline='') as f: (windows, python 3.9) Jun 6, 2021 at 7:54
36

Based in the answer of @G M and paying attention to the @John La Rooy's warning, I was able to append a new row opening the file in 'a'mode.

Even in windows, in order to avoid the newline problem, you must declare it as newline=''.

Now you can open the file in 'a'mode (without the b).

import csv

with open(r'names.csv', 'a', newline='') as csvfile:
    fieldnames = ['This','aNew']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

    writer.writerow({'This':'is', 'aNew':'Row'})

I didn't try with the regular writer (without the Dict), but I think that it'll be ok too.

0
21

If you use pandas, you can append your dataframes to an existing CSV file this way:

df.to_csv('log.csv', mode='a', index=False, header=False)

With mode='a' we ensure that we append, rather than overwrite, and with header=False we ensure that we append only the values of df rows, rather than header + values.

15

Are you opening the file with mode of 'a' instead of 'w'?

See Reading and Writing Files in the python docs

7.2. Reading and Writing Files

open() returns a file object, and is most commonly used with two arguments: open(filename, mode).

>>> f = open('workfile', 'w')
>>> print f <open file 'workfile', mode 'w' at 80a0960>

The first argument is a string containing the filename. The second argument is another string containing a few characters describing the way in which the file will be used. mode can be 'r' when the file will only be read, 'w' for only writing (an existing file with the same name will be erased), and 'a' opens the file for appending; any data written to the file is automatically added to the end. 'r+' opens the file for both reading and writing. The mode argument is optional; 'r' will be assumed if it’s omitted.

On Windows, 'b' appended to the mode opens the file in binary mode, so there are also modes like 'rb', 'wb', and 'r+b'. Python on Windows makes a distinction between text and binary files; the end-of-line characters in text files are automatically altered slightly when data is read or written. This behind-the-scenes modification to file data is fine for ASCII text files, but it’ll corrupt binary data like that in JPEG or EXE files. Be very careful to use binary mode when reading and writing such files. On Unix, it doesn’t hurt to append a 'b' to the mode, so you can use it platform-independently for all binary files.

2
  • 2
    may be you could make your answer more elaborated, then it would look like a real answer :-)
    – Areza
    Jul 29, 2011 at 12:10
  • 1
    @user, I added a link - does that help you? Jul 29, 2011 at 20:08
12

If the file exists and contains data, then it is possible to generate the fieldname parameter for csv.DictWriter automatically:

# read header automatically
with open(myFile, "r") as f:
    reader = csv.reader(f)
    for header in reader:
        break

# add row to CSV file
with open(myFile, "a", newline='') as f:
    writer = csv.DictWriter(f, fieldnames=header)
    writer.writerow(myDict)
11

I use the following approach to append a new line in a .csv file:

pose_x = 1 
pose_y = 2

with open('path-to-your-csv-file.csv', mode='a') as file_:
    file_.write("{},{}".format(pose_x, pose_y))
    file_.write("\n")  # Next line.

[NOTE]:

  • mode='a' is append mode.
2
  • Is it possible to loop thru rows and append a new row under each row this way?
    – santma
    Apr 30, 2021 at 19:50
  • 1
    @santma Yes, it is. You can use a for to do that. May 1, 2021 at 5:18
6
# I like using the codecs opening in a with 
field_names = ['latitude', 'longitude', 'date', 'user', 'text']
with codecs.open(filename,"ab", encoding='utf-8') as logfile:
    logger = csv.DictWriter(logfile, fieldnames=field_names)
    logger.writeheader()

# some more code stuff 

    for video in aList:
        video_result = {}                                     
        video_result['date'] = video['snippet']['publishedAt']
        video_result['user'] = video['id']
        video_result['text'] = video['snippet']['description'].encode('utf8')
        logger.writerow(video_result) 
2
  • 1
    Please fix your indentation. Answers also benefit from an explanation - and these answers also attract more upvotes.
    – Mr. T
    Mar 15, 2018 at 22:50
  • This is some simple code, it opens the file for append mode, writes out a header rec and then works through the video records in aList. Mar 21, 2018 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.