delete row from file using csv reader and lists python -
there similar questions on none deal specifics require.
i have following code seeks delete row in file, based on specified user input. methodology
- read file list
- delete relevant row in list (ideally while reading in list?)
- over-write file.
it's 2 , 3 guidance on comments best solution (for beginners, teaching/learning purposes) carry out sort of simple delete/edit in python csv reader.
code
""" ==============task 1. search given username 2. delete whole row particular user e.g. enter username: marvr >>the record marvr has been deleted file. """ import csv #1. code snippet asks user username , deletes user's record file. updatedlist=[] open("fakefacebook.txt",newline="") f: reader=csv.reader(f) username=input("enter username of user wish remove file:") row in reader: #for every row in file if username not in updatedlist: updatedlist=row #add each row, line line, list called 'udpatedlist' print(updatedlist) #delete row user list? #overwrite current file updated list? file contents:
username,password,email,no_of_likes marvr,pass123,marv@gmail.com,400 smithc,open123,cart@gmail.com,200 blogsj,2bg123,blog@gmail.com,99 update
based on answer below, have this, when overwrites file, doesn't update list correctly, not sure why.
import csv def main(): #1. code snippet asks user username , deletes user's record file. updatedlist=[] open("fakefacebook.txt",newline="") f: reader=csv.reader(f) username=input("enter username of user wish remove file:") row in reader: #for every row in file if row[0]!=username: #as long username not in row ....... updatedlist=row #add each row, line line, list called 'udpatedlist' print(updatedlist) updatefile(updatedlist) def updatefile(updatedlist): open("fakefacebook.txt","w",newline="") f: writer=csv.writer(f) writer.writerow(updatedlist) print("file has been updated") main() it appears print updatedfile correctly (as list) in removes username entered. on writing file, prints 1 username file.
any thoughts can accept final answer?
if username not in updatedlist:
to me should be:
if row[0] != username:
then in second loop write updatedlist csv file. personnally write in file while reading, in end delete old file , replace new one, makes 1 loop only.
edit:
replace updatedlist=row updatedlist.append(row): first 1 means overwriting updatedlist 1 row while second 1 means adding 1 more row it.
writerow writes 1 row, , give list of rows. use writerows instead , writing function work.
you made yourself, objective. other answers give better (faster, cleaner ...) ways, won't.
After filtering, the complete updatedlist needs to be written row by row using writer.writerows(updatedlist) rather than writer.writerow(updatedlist). The latter treats the entire list of rows as one CSV row, while writerows() writes each retained record separately, which is particularly relevant when developing practical Python Projects For Final Year.
ReplyDeleteThe example is therefore a good illustration of how CSV reading, list accumulation, conditional filtering, and file rewriting work together. Keeping these operations separate makes the logic easier for beginners to understand and also provides a foundation for more complete file-based applications using a Python Full Stack Developer Course.
ReplyDelete