Rock Lee
Rock Lee

Reputation: 9576

Trim whitespace from multiple lines

I tried to trim whitespace in python using s.strip() like this, but it's only working on the first line:

Input:

   a
    b

Output:

a
    b

How do I get it to trim whitespace from multiple lines? Here's my code:

Code:

import sys

if __name__ == "__main__":
    text_file = open("input.txt", "r")
    s = text_file.read()
    s = s.strip()
    text_file.close()
    with open("Output.txt", "w") as text_file:
        text_file.write(s)

Upvotes: 6

Views: 4747

Answers (4)

thoku
thoku

Reputation: 1130

Just for completeness, there is also textwrap.dedent(), which e.g. allows to write multi-line strings indented in code (for readability), while the resulting strings do not have left-hand side whitespaces. For example as given in https://docs.python.org/3/library/textwrap.html#textwrap.dedent

import textwrap

def test():
        # end first line with \ to avoid the empty line!
        s = '''\
        hello
          world
        '''
        print(repr(s))          # prints '    hello\n      world\n    '
        print(repr(textwrap.dedent(s)))  # prints 'hello\n  world\n'

Upvotes: 1

Anand S Kumar
Anand S Kumar

Reputation: 90969

The issue occurs because string.strip() only strips the trailing and leading whitespaces, it does not strip the whitespaces in the middle.

For the input -

   a
    b

And doing text_file.read() .

The actual string representation would be -

'   a\n    b'

s.strip() would strip the trailing and leading whitespaces , but not the \n and spaces in the middle, hence you are getting the multiple lines and the spaces in the middle are not getting removed.

For your case to work, you should read the input line by line and then strip each line and write it back.

Example -

import sys

if __name__ == "__main__":
    with open("input.txt", "r") as text_file, open("Output.txt", "w") as out_file:
        for line in text_file:
            out_file.write(line.strip() + '\n')

Upvotes: 3

Martijn Pieters
Martijn Pieters

Reputation: 1123430

Split the lines, strip each, then re-join:

s = text_file.read()
s = '\n'.join([line.strip() for line in s.splitlines()])

This uses the str.splitlines() method, together with the str.join() method to put the lines together again with newlines in between.

Better still, read the file line by line, process and write out in one go; that way you need far less memory for the whole process:

with open("input.txt", "r") as infile, open("Output.txt", "w") as outfile:
    for line in infile:
        outfile.write(line.strip() + '\n')

Upvotes: 13

Himanshu Mishra
Himanshu Mishra

Reputation: 9416

Use

for line in s.splitlines()

to iterate over each line and use strip() for them.

Upvotes: 3

Related Questions