Hi I am trying to make a function which writes a table in a csv file.
But writerows and writerow just and an another table without clearing the first content(unLike in txt files)
here is the class and the program
import csv as c class Table: def __init__(self,File): self.file = File self.ro = c.reader(File) self.wo = c.writer(File) self.table_list = [] for row in self.ro: self.table_list.append(row) def get_table_list(self): . . . def visualize_table(self): . . . def get_number_of_column(self): . . . def get_number_of_row(self): . . . def get_index(self,item_entry,item): . . . def change(self,item_entry,item,to_item): . . . def write_table(self): self.wo.writerows(self.table_list) file = open("test.csv","r+",newline="") table = Table(file) table.change("name","tarun","aditya") table.write_table() file.close()
As you can see this is not what I want
please help me I cannot use any other module like pandas etc
Advertisement
Answer
You can use the truncate method.
>>> file = open("test.csv","r+",newline="") >>> file.truncate(0) # Clears file