I want to delete rows from a csv file as they are processed. My file:
Sr,Name1,Name2,Name3
1,Zname1,Zname2,Zname3
2,Yname1,Yname2,Yname3
3,Xname1,Xname2,Xname3
I want to read row by row and delete the row which has been processed. So the file will be now:
2,Yname1,Yname2,Yname3
3,Xname1,Xname2,Xname3
The solutions which are provided on other questions are:
- read the file
- use
next()
or any other way to skip the row and write the remaining rows in an updated file
I want to delete the row from the original file which was entered in .reader()
method
My code:
with open("file.txt", "r") as file
reader = csv.reader(file)
for row in reader:
#process the row
#delete the row
I have not been able to figure out how to delete/remove the row.
I want the change to be in the original file.txt
because I will be running the program many times and so each time it runs, file.txt
will already be present and the program will start from where it ended the last time.