Sara
Sara

Reputation: 595

Removing spaces and empty lines from a file Using Python

I have a file which contains a value 2000,00.

But it contains spaces after 2000,00 and empty lines.

I want to remove all the spaces and empty lines, if some one can give some Idea, I ave tried a number of ways but no success.

One method I tired is as below

    # Read lines as a list
fh = open("transfer-out/" + file, "r")
lines = fh.readlines()
fh.close()
# Weed out blank lines with filter
lines = filter(lambda x: not x.isspace(), lines)
# Write "transfer-out/"+file+".txt", "w"
fh = open("transfer-out/"+file, "w")
#fh.write("".join(lines))
# should also work instead of joining the list:
fh.writelines(lines)
fh.close()

Upvotes: 6

Views: 71396

Answers (5)

Jill-Jênn Vie
Jill-Jênn Vie

Reputation: 1841

strip() removes leading and trailing whitespace characters.

with open("transfer-out/" + file, "r") as f:
    for line in f:
        cleanedLine = line.strip()
        if cleanedLine: # is not empty
            print(cleanedLine)

Then you can redirect the script into a file python clean_number.py > file.txt, for example.

Upvotes: 8

Kirill
Kirill

Reputation: 3454

Functional one :)

import string
from itertools import ifilter, imap

print '\n'.join(ifilter(None, imap(string.strip, open('data.txt'))))
# for big files use manual loop over lines instead of join

Usage:

$ yes "2000,00  " | head -n 100000 > data.txt
$ python -c "print '\n'*100000" >> data.txt
$ wc -l data.txt 
200001 data.txt
$ python filt.py > output.txt
$ wc -l output.txt 
100000 output.txt

Upvotes: 1

Diego Navarro
Diego Navarro

Reputation: 9704

Another one with list comprehension:

clean_lines = []
with open("transfer-out/" + file, "r") as f:
    lines = f.readlines()
    clean_lines = [l.strip() for l in lines if l.strip()]

with open("transfer-out/"+file, "w") as f:
    f.writelines('\n'.join(clean_lines))

Upvotes: 4

Jiri
Jiri

Reputation: 16625

This should work as you wish:

file(filename_out, "w").write(file(filename_in).read().strip())

EDIT: Although previous code works in python 2.x, it does not work python 3 (see @gnibbler comment) For both version use this:

open(filename_out, "w").write(open(filename_in).read().strip())

Upvotes: 1

Simon Peverett
Simon Peverett

Reputation: 4207

Change your 'lines' line to use the following generator and it should do the trick.

lines = (line.strip() for line in fh.readlines() if len(line.strip()))

Upvotes: 2

Related Questions