Why doesn't .rstrip('\n') work?

line, python

Solution

`str.rstrip()` removes the trailing newline, not all the newlines in the middle. You have one long string, after all.

Use `str.splitlines()` to split your document into lines without newlines; you can rejoin it if you want to:

doclines = doc.splitlines()
doc_rejoined = ''.join(doclines)

but now `doc_rejoined` will have all lines running together without a delimiter.

Problem

Let's say `doc.txt` contains ``` a b c d ``` and that my code is ``` f = open('doc.txt') doc = f.read() doc = doc.rstrip('\n') print doc ``` why do I get the same values?

Original source