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.
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.
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.
Append to list each line of CSV file row if row is have numeric values
Python Append/ Insert Filenames to Existing CSV
Append new field names to csv file
Edit/Append data to csv file
hey, everyone good day! (I'm very new to this coding stuff)
I was writing a simple program and this program needs to append new data from users to the existing csv file so I wrote this function.
def append_list_as_row(file_name, list_of_elem):
with open(file_name, 'a+', newline='') as write_obj:
csv_writer = writer(write_obj)
csv_writer.writerow(list_of_elem)this just works fine until the last element from the csv file is 0.
let's say we have [Robert][19][male][173] this kind of data in excel. and this works just fine with the function. but when it's like [Robert][19][male][0], the next appended data will not generate a new row and will continue adding data in the current row and replacing 0 to the first data element from the user.
I hope you guys understand my English...welp, anyways I want this function to work seemly whether the last element is 0 or not. I've been searching the internet for quite a long but I was not able to find the answer. is there any kind soul who can help me?