Add new line to old python csv file

I am trying to add a new line to my old csv file. Essentially, it is updated every time I run a Python script.

I now store the old csv string values ​​in the list, and then delete the csv file and create it again with the new list value.

I would like to know if there are better ways to do this.

+167
python append csv writer
Mar 02
source share
7 answers
with open('document.csv','a') as fd: fd.write(myCsvRow) 

Opening a file with the 'a' option allows you to add it to the end of the file instead of simply overwriting existing content. Try this.

+210
Mar 02 '10 at 14:25
source share

I prefer this solution using the csv module from the standard library and the with statement to keep the file open.

The key point is to use 'a' to add when opening a 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 might get extra new lines on Windows. You can try to avoid them by using 'ab' instead of 'a' , however this will raise a TypeError: a byte-like object is required, not 'str' in python and CSV in Python 3.6 when adding newline='' as Natasha said in Python 3.6, You will encounter problems of backward compatibility of the text binary mode of the Python 2 and 3 csv module .

+118
Jun 06 '16 at 9:46 on
source share

Based on @GM's answer and paying attention to @John La Rooy's warning, I was able to add a new line that opens the file in 'a' mode.

Even on Windows, to avoid a problem with a new line, you must declare it as newline='' .

Now you can open the file in 'a' mode (without 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 have not tried with a regular writer (without a Dict), but I think it will be fine.

+16
Jun 21. '18 at 16:47
source share

Do you open a file with the mode 'a' instead of 'w'?

See Reading and writing files in python docs

7.2. Reading and writing files

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

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

The first argument is a string containing the file name. The second argument is another line containing several characters describing the way the file will be used. the mode can be "r" when the file is read only, 'w' is for writing only (an existing file with the same name will be erased), and 'a' opens the file for adding; any data the file is automatically added to the end. 'r +' opens the file for both reading and writing. The mode parameter is optional; 'r' will be assumed if omitted.

On Windows, the 'b' added to mode opens the file in binary mode, so there are also modes such as "rb", "wb" and "r + b". Python on Windows makes a distinction between text and binary; end of line characters in text files automatically change when data is read or written. This behind-the-scenes modification of file data is great for ASCII text files, but itll corrupt binary data like this in JPEG or EXE files. Be very careful when using binary mode when reading and writing such files. On Unix, it doesn't hurt to add "b" to mode, so you can use its platform-independently for all binaries.

+12
Mar 02
source share
 # 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) 
+4
Mar 15 '18 at 20:57
source share

If the file exists and contains data, then you can 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) 
+4
Feb 06 '19 at 11:52
source share

I do this to add a new line to the 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") 
+1
Jul 18 '19 at 18:37
source share



All Articles