https://www.geeksforgeeks.org/how-to-append-a-new-row-to-an-existing-csv-file/
import csv
import os
class WriteRowToCsv:
def __init__(self) -> None:
pass
def get_writer(self,file_path):
if os.path.exists(file_path):
self.file_value = open(file_path,'a',encoding="utf-8", newline='')
self.csv = csv.writer(self.file_value)
else:
self.file_value = open(file_path,'w',encoding="utf-8", newline='')
self.csv = csv.writer(self.file_value)
def write_new_row_by_list(self,value):
self.csv.writerow(value)
def save(self):
self.file_value.close()
def get_dict_writer(self,file_path,fieldnames:list):
if os.path.exists(file_path):
self.file_value = open(file_path,'a',encoding="utf-8", newline='')
self.csv = csv.DictWriter(self.file_value,fieldnames)
else:
self.file_value = open(file_path,'w',encoding="utf-8", newline='')
self.csv = csv.DictWriter(self.file_value,fieldnames)
def write_new_value_by_dict(self,value:dict):
self.csv.writerow(value)
instance = WriteRowToCsv()
instance.get_writer(r'C:\Users\84977\Desktop\ellistest.csv')
instance.write_new_row_by_list(['a','b','c'])
instance.write_new_row_by_list(['1','2','3'])
instance.save()