0

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?

  • What specifically is the issue? Have you done any debugging? Also, is that data CSV? If so, why not use the csv library? – AMC Apr 23 '20 at 23:32

3 Answers3

0

One way is:

with open("theFile.txt") as f:
  items = list(f)

name = input("Enter Name to delete record:\n") # input is always a string

with open("theFile.txt", "w") as f:
  [f.write(x) for x in items if not x.split(",")[0] == name]

The last line can also be written as:

for line in items:
    parts = line.split(",")
    if not parts[0] == name:
        f.write(line)

Demo

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • 1
    Hi thanks! This works. Do you mind explaining how this works? – Basir Nurzai Apr 23 '20 at 22:59
  • You're welcome, the most important line is the last, a [list comprehension](https://www.pythonforbeginners.com/basics/list-comprehensions-in-python), where we loop the contents of `theFile.txt`, split the lines on `,` and check if the `name` doesn't match the 1st item on the list generated by the `split`, if so, we can write to file. It's actually easier for me to code than explain ;), but I think you get the point. – Pedro Lobito Apr 23 '20 at 23:03
  • 1
    Yes, I get the general idea. Thank you very much! – Basir Nurzai Apr 23 '20 at 23:12
  • Are you using a list comprehension for side effects? – AMC Apr 23 '20 at 23:33
  • @PedroLobito I was concerned about clear and sensible code, not performance. In fact, the QA you linked to is asking whether or not it's considered good style, and the answer is a resounding no. You could always use `writelines()`, if for whatever reason you want to keep it on a single line. – AMC Apr 24 '20 at 00:03
0
name = str(input("Enter Name to delete record:\n"))
all_lines=[]
with open("theFile.txt", "r") as file:
    for x in file:
        all_lines.append(x.strip("\n"))
for line in all_lines:
    seperated = line.split(',')
    if seperated[0] == name:
        all_lines.remove(line)
        print("Row removed")

with open("theFile.txt","w") as f:
    for line in all_lines:
        f.write(str(line)+"\n")
Brian Brix
  • 449
  • 4
  • 12
0

It should work like this:

name = str(input("Enter Name to delete record:\n"))
lines=[]
with open("theFile.txt", 'r') as file:
    for x in file:
        seperated = x.split(',')
        if seperated[0] == name:
            print(seperated)
            print("Row removed")
        else:
            lines.append(x)


with open("theFile.txt", "w") as file:
    for line in lines:
        print(line)
        file.write(line)
Khaled Adrani
  • 171
  • 3
  • 7