tags:

views:

95

answers:

6

For example, we have some file like that:

first line
second line

third line

And in result we have to get:

first line
second line
third line

Use ONLY python

+1  A: 
>>> s = """first line
... second line
... 
... third line
... """
>>> print '\n'.join([i for i in s.split('\n') if len(i) > 0])
first line
second line
third line
>>> 
Pydev UA
It depends on what "blank" means - this only works if blank means "nothing at all". If there are spaces between second line and third line, this will fail. Plus it needs to work on files :) But I like that you didn't have to import regexps :)
Chirael
@Chirael - for that case you may add just len(i.strip()) > 0
Pydev UA
+4  A: 
import fileinput
for line in fileinput.FileInput("file",inplace=1):
    if line.rstrip():
        print line
ghostdog74
+1 for also catching lines that contain whitespace and nothing else.
Tim Pietzcker
This will change for formatting of whitespace even in the good lines
Thomas Ahle
don't understand, like what formatting? care to elaborate?
ghostdog74
Markdown formatting utilises trailing spaces. Remove simple change to this answer would strip lines with just whitespace and preserve trailing spaces: `if line.rstrip(): print line`
MattH
Sure, imagine a tab seperated table, where not all fields have a value: `a\tb\t\tc\t\n` `d\te\t\t\tf\n` `\t\tg\th\ti\n`
Thomas Ahle
@Thomas, and why would a field have an ending "\n" in a file? If a file has "\n", then i would bet its literal. If its really a "\n", then the next field will be on the next line. isn't that so? or am i still missing what you are saying? If its necessarily pls provide your explanation as an answer as the putting in comment is hard to read.
ghostdog74
@ghostdog74 It's not about the line breaks, but about the tabs. If you cut the tabs from the end of each line, then each row in the table will not have the same number of columns.
Thomas Ahle
+1  A: 

I know you asked about Python, but your comment about Win and Linux indicates that you're after cross-platform-ness, and Perl is at least as cross-platform as Python. You can do this easily with one line of Perl on the command line, no scripts necessary: perl -ne 'print if /\S/' foo.txt

(I love Python and prefer it to Perl 99% of the time, but sometimes I really wish I could do command-line scripts with it as you can with the -e switch to Perl!)

That said, the following Python script should work. If you expect to do this often or for big files, it should be optimized with compiling the regular expressions too.

#!/usr/bin/python
import re
file = open('foo.txt', 'r')
for line in file.readlines():
    if re.search('\S', line): print line,
file.close()

There are lots of ways to do this, that's just one :)

Chirael
You can do commandline scripts with python using the `-c` flag. Unfortunately you would have to use multiple lines (or seperation with ;) in order to read from standard input.
Thomas Ahle
+3  A: 

The with statement is excellent for automatically opening and closing files.

with open('myfile','rw') as file:
    for line in file:
        if line.strip():
            file.write(line)
Thomas Ahle
+1 for use of "with" and good, pythonic iteration through lines, in addition to not mutating the good output lines.
Michael Aaron Safyan
+1  A: 
import sys
with open("file.txt") as f:
    for line in f:
        if not line.isspace():
            sys.stdout.write(line)

Another way is

with open("file.txt") as f:
    print "".join(line for line in f if not line.isspace())
gnibbler
A: 

Have you tried something like the program below?

for line in open(filename):
    if len(line) > 1 or line != '\n':
        print(line, end='')
Noctis Skytower