August 17, 2018
Remove blank lines from file in python
Introduction
Removing blank lines from a file requires two steps. First, we need to loop through the file lines. Second, check if the line is empty. In Python, there are different ways to perform the loop and check.
Let us summarize that…
Loop method
There are multiple styles to iterate through file lines. Here are some examples…
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# Style 1 with open('empty.txt','r') as f: for line in f: do # Style 2 with open('empty.txt','r') as f: for line in f.readlines(): do # Style 3 for line in open('empty.txt'): do |
Check for empty lines
Checking for blanks can be achieved in various ways. Take a look…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# Before checking we strip leading and trailing characters # Check if the line is not empty with open("empty.txt", 'r') as inp, open('out.txt', 'w') as out: for line in inp: if line.strip(): out.write(line) # Before checking we strip only trailing characters # Check if the line is not empty with open("empty.txt", 'r') as inp, open('out.txt', 'w') as out: for line in inp: if line.rstrip(): out.write(line) # Check if the length is greater than zero with open("empty.txt", 'r') as inp, open('out.txt', 'w') as out: for line in inp: if len(line.strip()) > 0: out.write(line) # Check if the line is not a new line with open("empty.txt", 'r') as inp, open('out.txt', 'w') as out: for line in inp: if line != '\n': out.write(line) # Check if the line is not a space using a built in function with open("empty.txt", 'r') as inp, open('out.txt', 'w') as out: for line in inp: if not line.isspace(): out.write(line) # Check if the line is not white space using regular expressions import re with open("empty.txt", 'r') as inp, open('out.txt', 'w') as out: for line in inp: if re.search('\S', line): out.write(line) |
One liner implementations
We can utilize Python elegance and write one liner implementations. Check out these…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# Open out.txt for writing # Read empty.txt into a string # Replace white space with new line characters import re open('out.txt','w').write( re.sub('\n\s*\n','\n', open('empty.txt').read())) # Get all lines from empty.txt and join them # into one string then write to out.txt open('out.txt','w').write( ''.join( l for l in open( 'empty.txt') if l.strip())) # Same as above but in this case the check # is different. l[:-1] means the whole line # except the last character. New lines get # removed this way. Note that this may not # work in case of blank lines with white space open('out.txt','w').write( ''.join( l for l in open( 'empty.txt') if l[:-1])) |
That is all for today. Thanks for visiting. Please use the comments section below for feedback
More from my site
One Comment
I tried all of them but never could delete the blank lines in the last line.