Let's say I have a text file containing the following details:
name1,address,more details
name2,address,more details
name3,address,more details
I take an input and it finds the row like this:
name = str(input("Enter Name to delete record:\n"))
with open("theFile.txt", "r") as file:
for x in file:
seperated = x.split(',')
if seperated[0] == name:
print(x)
This prints the whole row of the details. Now here's the part I'm stuck on. How can I delete this row? I've tried this but it doesn't do anything to the file.
with open("theFile.txt", "r") as file:
lines = file.readlines()
with open("theFile.txt", "w") as file:
for line in lines:
if line.strip("\n") != x:
file.write(line)
else:
print("Row removed")
I've seen other posts where you can remove the row by comparing it to the whole row but how can I do this by comparing the first index only?