6

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()
2
  • 1
    Without Python: cat input.txt | egrep -v '^\s*$' > output.txt Commented May 29, 2012 at 6:45
  • 1
    This leaves the spaces after the number. Maybe add a tr: cat input.txt | egrep -v '^\s*$' | tr -d ' ' > output.txt Commented May 29, 2012 at 7:01

5 Answers 5

8

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Factor out those repeated strip() calls, and you're onto a winner. :-)
It's just the name of the directory containing the file named file.
4

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))

1 Comment

You should add a join: f.writelines('\n'.join(clean_lines)).
2

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()))

1 Comment

if len(line.strip()) can be replaced with if line.strip()
1

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())

11 Comments

I think you meant open not file here.
No. I meant file. I prefer file, but according to doc (docs.python.org/library/functions.html?highlight=file#file) open is prefferable :-(
@BurhanKhalid, file is a deprecated synonym for open. It exists in Python2 because open is a strange name for subclassing file or using with isinstance. It no longer exists in Python3
@Jiri what is filename_out and filename_in ?
@gnibbler Oh, yes, I am using 2.x most of the time and I have not realized this! I think it's time to use open instead of file.
|
1

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

2 Comments

I am sorry I am to dumb to read your code ! I have to accesss my file from a folder and removing spaces and empty lines from it !
and yeh one more thing write data back to the same file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.