How do I remove the "\n" characters when reading my file but deleting one variable then replacing it

Viewed 64

I am trying to make a program that reads from a file and deletes one specific line inside of it and then puts all the data stored back to the file separated with a new line. The file uses this format:

Jones|20|20|00
bob|30|19|90
James|40|19|80

So I want to delete (backup contains this and is the line I want to delete)
bob|30|19|90
but the code that I am using takes away the new line and doesnt replace it but when I try to add \n to it the file doesn't want to read as it does this (adds 2 "\n"s):

Jones|20|20|00

James|40|19|80

I am using this code below:

def deleteccsaver(backup):
lockaccount =""
lockaccount = lockaccount.strip("\n")
with open('accounts_project.txt','r+') as f:
    newline=[]
    for line in f.readlines():
        newline.append(line.replace(backup, lockaccount).strip("\n"))
with open('accounts_project.txt','w+') as f:
    for line in newline:
            f.writelines(line +"\n")
    f.close()
    resetlogin()

Please help as I dont know how to add the \n back without it appearing as "\n\n"
Without the "\n "it appears as:

Jones|20|20|00James|40|19|80

Any suggestions:

4 Answers

What I am doing here is reading the entire file at once, please don't do this if you have a very very big file. After reading all file contents at once, I am making a list out of it using "\n" as a delimiter. Read about split function in python to know more about it. Then from the list I am replacing the backup with lockaccount, as you have been doing the same, these are the names of variables that you are using, hope I did not confuse between them in this case. Then it will be saved to a new file after adding new line after each element of list, i.e. each line of the previous file. This will cause the result file to have all the contents as previous file, but removing what you wanted to remove. I see that lockaccount is itself an empty string, so adding it might create a newline in your file. In case you dont want lockaccount to replace the backup variable in the file, just remove the backup from the list using contents.remove(backup) instead of contents[contents.index(backup)] == lockaccount keeping the rest of the code same. Hope this explains better.

def deleteccsaver(backup):
    lockaccount =""
    lockaccount = lockaccount.strip("\n")
    with open('accounts_project.txt','r+') as f:
        contents = f.read().split("\n")
        if backup in contents:
            contents[contents.index(backup)] = lockaccount
        new_contents = "\n".join(contents)
    with open('accounts_project.txt','w+') as f:
        f.write(new_contents)
    resetlogin()

You are priting a newline character after each element in the list. So, if you replace a line with the empty string, well, you will get an empty line.

Try to simply skip over the line you want to delete:

if line == backup:
    contiune
else:
    lines.append(...)

PS. There is room for improvment in the code above, but I'm on the phone, I will get back with an edit later if nobody gets ahead of me

You can try to add newline = '\n'.join(newline) after your first for loop and then just write it into the accounts_project.txt file without a loop. The code should then look like:

def deleteccsaver(backup):
lockaccount =""
lockaccount = lockaccount.strip("\n")
with open('accounts_project.txt','r+') as f:
    newline=[]
    for line in f.readlines():
        newline.append(line.replace(backup, lockaccount).strip("\n"))
    newline = '\n'.join(newline)
with open('accounts_project.txt','w+') as f:
    f.write(newline)
    f.close() # you don't necessarily need it inside a with statement
    resetlogin()

Edit:

Above code still results in

Jones|20|20|00

James|40|19|80

as output.

That's because during the replacement loop an empty string will be appended to newline (like newline: ['Jones|20|20|00','','James|40|19|80']) and newline = '\n'.join(newline) will then result in 'Jones|20|20|00\n\nJames|40|19|80'.

A possible fix can be to replace:

for line in f.readlines():
    newline.append(line.replace(backup, lockaccount).strip("\n"))

with

for line in f.readlines():
    line = line.strip('\n')
    if line != backup:
        newline.append(line)
def deleteccsaver(backup):
lockaccount =""
lockaccount = lockaccount.strip("\n")
with open('accounts_project.txt','r+') as f:
    contents = f.read().split("\n")
    if backup in contents:
        contents.remove(backup)
    new_contents = "\n".join(contents)
with open('accounts_project.txt','w+') as f:
    f.write(new_contents)
resetlogin()
Related