Remove spaces before newlines
<p>I need to remove all spaces before the newline character throughout a string.</p>

<pre><code>string = """
this is a line       \n
this is another           \n
"""
</code></pre>

<p>output:</p>

<pre><code>string = """
this is a line\n
this is another\n
"""
</code></pre>

<pre><code>import re
re.sub('\s+\n','\n',string)
</code></pre>

<p>Edit: better version from comments:</p>

<pre><code>re.sub(r'\s+$', '', string, flags=re.M)
</code></pre>

python