-1

I have a text file called Accounts.txt. It stores usernames and passwords in plain text like this:

Username Password
Username2 Password2
Username3 Password3etc

My code is this:

with open("Accounts.txt", "a+") as Accounts:
    Accounts.write("LoginName") #write password to Accounts.txt
    Accounts.write(" ")
    Accounts.write("Password") #write password to Accounts.txt
    Accounts.write("\n") #create new line
    Accounts.seek(0) #start reading from start of txt
    lines = str(Accounts.readlines())
    cleanedlines = lines.replace("\n","")
    print(cleanedlines, end = "")

The output of running this is this:

C:\Users\drago\Desktop\PythonPractice\venv\Scripts\python.exe C:\Users\drago\Desktop\PythonPractice\FileAppendTest.py 
['mrwong poop123\n', 'edible_man weeeeed420\n', 'portarossa pigsanimalsgood\n', 'LoginName Password\n', 'LoginName Password\n', 'LoginName Password\n', 'LoginName Password\n', 'LoginName Password\n', 'LoginName Password\n']
Process finished with exit code 0

How do I remove all the \n?

I have tried lines.replace("\n","") and it did not work. Edit: Also using strip and rstrip has not worked. Im on pycharm.

aw92
  • 1
  • 1
  • Does this answer your question? [Getting rid of \n when using .readlines()](https://stackoverflow.com/questions/15233340/getting-rid-of-n-when-using-readlines) – mkrieger1 Mar 10 '23 at 00:36
  • Have you used `strip` and `rstrip` like it is shown in the answers to the other question, or differently? – mkrieger1 Mar 10 '23 at 00:52
  • No. Still seeing /n. – aw92 Mar 10 '23 at 00:53
  • ```with open("Accounts.txt", "a+") as Accounts: Accounts.write("LoginName") Accounts.write(" ") Accounts.write("Password") Accounts.write("\n") Accounts.seek(0) #start reading from start of txt lines = str(Accounts.readlines()) cleanedlines = lines.rstrip("\n") print(cleanedlines, end = "")``` – aw92 Mar 10 '23 at 00:53
  • You should have done it like shown in the answers to the other question instead. – mkrieger1 Mar 10 '23 at 00:53
  • `cleanedlines` is the string representation of a list; it doesn't contain any newlines; it contains literal digraphs consistency of ``\`` and `n`. – chepner Mar 10 '23 at 17:59

2 Answers2

0
with open("C:\\Users\\jjnit\\Downloads\\test.txt", "a+") as Accounts:
    Accounts.write("LoginName") #write password to Accounts.txt
    Accounts.write(" ")
    Accounts.write("Password") #write password to Accounts.txt
    Accounts.write("\n") #create new line
    Accounts.seek(0) #start reading from start of txt
    lines = Accounts.readlines()
    new_lines = [lines[i].strip("\n") for i in range(len(lines))]
    print(new_lines, end = "")

Your issue was that it was only replacing the first "\n". That was because you did lines = str(Accounts.readlines()). When you converted it to a string, you changed the format to: '["a", "b", "c", "d", "a"]'. When this happends, doing .replace["b"] will only replace the first one. Because of the repetive "\n", only the first one got replaced, while all the others stayed the same. Additionally, when you did the .replace("\n", " "), the format became something like this "["a ' '", "b ' '", "c ' '", "d ' '"]" Anyway, I hope this solved your issue. If you are applying this for something specific, then I can reformat the code to be more efficient if needed (though it's easiest to understand right now)

Jonathan
  • 185
  • 9
  • 1
    Fixed it, sorry, I pasted the wrong code in originally (I design all my stack overflow post answers together in a python file). That was a test, not the actual answer. I edited it to be the correct thing. – Jonathan Mar 10 '23 at 17:51
0

You don't want to generate a single string representation of the list of values. You want to remove the newlines from each element of the list.

lines = Accounts.readlines())
cleanedlines = [x.rstrip('\n') for x in lines]
print(cleanedlines, end = "")
chepner
  • 497,756
  • 71
  • 530
  • 681