-1

I am reading a file in python and getting the lines from it. However, after printing out the values I get, I realize that after each line there is a trailing \ at the end. I have looked at Python strip with \n and tried everything in it but nothing has removed the trailing . For example

0048\

0051\

0052\

0054\

0056\

0057\

0058\

0059\

How can I get rid of these slashes?

Here is the code I have so far

for line in f:
    line = line.replace('\\n', "")
    line = line.replace('\\n', "") 
    print(line)

I've even tried using regex

strings = re.findall(r"\S+", f.read())

But nothing has worked so far.

Snow
  • 1,058
  • 2
  • 19
  • 47
Sreehari R
  • 919
  • 4
  • 11
  • 21
  • I tried using line = line.rstrip() however I still get the backslash at the end. I think line = line.rstrip() just removes the trailing spaces. – Sreehari R Jul 13 '17 at 10:00
  • 2
    `line.replace('\\n', "")`? Why are you trying to remove only backslashes that are followed by an `n` character? – Stefan Pochmann Jul 13 '17 at 10:05

1 Answers1

1

You're probably confused about what is in the lines, and as a result you're confusing me too. '\n' is a single newline character, as shown using repr() (which is your friend when you want to know what a value is exactly). A line typically ends with that (the exception being the end of file which might not). That does not contain a backslash; that backslash is part of a string literal escape sequence. Your replace argument of '\\n' contains two characters, a backslash followed by the letter n. This wouldn't match a '\n'; the easiest way to remove the newline specifically is to use str.rstrip('\n'). The line reading itself will guarantee that there's only up to one newline, and it is at the end of the string. Frequently we use strip() with no argument instead as we don't want whitespace either.

If your string really does contain backslash, you can process that as well, whether using replace, strip, re or some other string processing. Just keep in mind that it might be used for escape sequences not only at string literal level but at regular expression level too. For instance, re.sub(r'\\$', '', str) will remove a backslash from the end of a string; the backslash itself is doubled to not mean a special sequence in the regular expression, and the string literal is raw to not need another doubling of the backslashes.

Yann Vernier
  • 15,414
  • 2
  • 28
  • 26
  • If it were a newline character, the `print(line)` would print a newline character, not a backslash. And something like `print(repr(line))` would print the `n` in addition to the backslash. And the `re.findall(r"\S+", f.read())` would've worked. So pretty sure it's really just a backslash. – Stefan Pochmann Jul 13 '17 at 10:13
  • Either way a repr() call would clarify the situation, and that re.sub would be able to pick that backslash out if it is a trailing backslash. – Yann Vernier Jul 13 '17 at 10:14
  • I mean it would address specifically a trailing backslash. Unlike a replace, it doesn't touch any other backslashes, and it even leaves the `'\n'` alone if there is one. That's likely not necessary, but I have an incomplete picture of what needs to be done. – Yann Vernier Jul 13 '17 at 10:17
  • Oh, sorry, I just realized you're not talking about the OP's `re.findall` that I mentioned (I thought you responded to that) but about your own `re.sub`. – Stefan Pochmann Jul 13 '17 at 10:23