Tyler Durden
Tyler Durden

Reputation: 107

Remove whitespaces in the beginning of every string in a file in python?

How to remove whitespaces in the beginning of every string in a file with python?

I have a file myfile.txt with the strings as shown below in it:

 _ _ Amazon.inc  
Arab emirates
 _ Zynga  
Anglo-Indian  

Those underscores are spaces.

The code must be in a way that it must go through each and every line of a file and remove all those whitespaces, in the beginning of a line.

I've tried using lstrip but that's not working for multiple lines and readlines() too.

Using a for loop can make it better?

Upvotes: 0

Views: 2640

Answers (3)

Martin Geisler
Martin Geisler

Reputation: 73788

To read the lines from myfile.txt and write them to output.txt, use

with open("myfile.txt") as input:
    with open("output.txt", "w") as output:
        for line in input:
            output.write(line.lstrip())

That will make sure that you close the files after you're done with them, and it'll make sure that you only keep a single line in memory at a time.

The above code works in Python 2.5 and later because of the with keyword. For Python 2.4 you can use

input = open("myfile.txt")
output = open("output.txt", "w")
for line in input:
    output.write(line.lstrip())

if this is just a small script where the files will be closed automatically at the end. If this is part of a larger program, then you'll want to explicitly close the files like this:

input = open("myfile.txt")
try:
    output = open("output.txt", "w")
    try:
        for line in input:
            output.write(line.lstrip())
    finally:
        output.close()
finally:
    input.close()

You say you already tried with lstrip and that it didn't work for multiple lines. The "trick" is to run lstrip on each individual line line I do above. You can try the code out online if you want.

Upvotes: 1

jcollado
jcollado

Reputation: 40414

All you need to do is read the lines of the file one by one and remove the leading whitespace for each line. After that, you can join again the lines and you'll get back the original text without the whitespace:

with open('myfile.txt') as f:
    line_lst = [line.lstrip() for line in f.readlines()]
    lines = ''.join(line_lst)

print lines

Upvotes: 4

David Robinson
David Robinson

Reputation: 78620

Assuming that your input data is in infile.txt, and you want to write this file to output.txt, it is easiest to use a list comprehension:

inf = open("infile.txt")
stripped_lines = [l.lstrip() for l in inf.readlines()]
inf.close()

# write the new, stripped lines to a file
outf = open("output.txt", "w")
outf.write("".join(stripped_lines))
outf.close()

Upvotes: 2

Related Questions