2

I have a small question. I noticed that, for some reason, when I use the + symbol when joining two variables, Python automatically uses a newline.

for i in range(o):
    a = Before.readline()
    b = After.readline()
    if a == b:
        lines.append(" \n")
    else:
        plus = a + b
        lines.append(a + b)

Final.writelines(lines)

This would result in a list with values as such (Notice the 'B\nC\n')

[' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n',
 ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n',
 'B\nC\n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n']

Assuming that I have initialized the files Before, After, and Final correctly, what should I do to remove these newlines and just add a space? I would prefer to do this without using many libraries.

EDIT : I do know about the .strip() method. It is indeed very helpful, to remove the new lines. However, I seem to have phrased my question a little wrong. I was also wondering about how to add the new lines, as a + ' ' + b doesn't really seem to work. How would I do this?

DOUBLE EDIT : I'm stupid. I put the wrong variable in the appending area. Nevermind, and thanks anyways!

  • Python includes the trailing line end character(s) on a `readline()`. – Klaus D. Oct 16 '18 at 02:11
  • 1
    Possible duplicate of [Getting rid of \n when using .readlines()](https://stackoverflow.com/questions/15233340/getting-rid-of-n-when-using-readlines) – metatoaster Oct 16 '18 at 02:11
  • @metatoaster Perhaps, although the OP is reading line by line, and the top answer in your suggested duplicate suggests using `.splitlines`, which reads the whole thing into a list, and the OP may not want to do that. OTOH, other answers, eg Martijn Pieter's show how to trim lines one by one. – PM 2Ring Oct 16 '18 at 02:19
  • @PM2Ring Yes, sorry. I appear to have written the wrong thing. You are indeed right. – Shreyas Shridharan Oct 16 '18 at 02:28

1 Answers1

5

The Before.readline() and After.readline() are including the newlines in the file you are reading. To remove the trailing newlines and whitespaces, you can:

Before.readline().strip()

Then if you want to add newlines in your a+b line, you will need to format it explicitly how you want, in any way you want. For example:

  • a + ' ' + b
  • "{} {}".format(a,b)
  • a + b + "\n"
  • etc

You could also only perform .strip() on a when you are adding it to the list. So many possibilities!

LTClipp
  • 526
  • 1
  • 4
  • 14
  • 1
    To remove only newline characters at the end, `rstrip('\n')` should to be used. – martineau Oct 16 '18 at 02:19
  • This is the right method to remove the newline, but how would I get the space in the middle? I can't seem to get that with the first bullet. Edit : Nevermind, I forgot a bit. Thanks for the answer! – Shreyas Shridharan Oct 16 '18 at 02:22
  • How does the first bullet not work? It explicitly adds a space in the middle. – LTClipp Oct 16 '18 at 02:30