Easiest Way to Ignore Blank Lines When Reading a File in Python

Easiest way to ignore blank lines when reading a file in Python

I would stack generator expressions:

with open(filename) as f_in:
lines = (line.rstrip() for line in f_in) # All lines including the blank ones
lines = (line for line in lines if line) # Non-blank lines

Now, lines is all of the non-blank lines. This will save you from having to call strip on the line twice. If you want a list of lines, then you can just do:

with open(filename) as f_in:
lines = (line.rstrip() for line in f_in)
lines = list(line for line in lines if line) # Non-blank lines in a list

You can also do it in a one-liner (exluding with statement) but it's no more efficient and harder to read:

with open(filename) as f_in:
lines = list(line for line in (l.strip() for l in f_in) if line)
Update:

I agree that this is ugly because of the repetition of tokens. You could just write a generator if you prefer:

def nonblank_lines(f):
for l in f:
line = l.rstrip()
if line:
yield line

Then call it like:

with open(filename) as f_in:
for line in nonblank_lines(f_in):
# Stuff
update 2:

with open(filename) as f_in:
lines = filter(None, (line.rstrip() for line in f_in))

and on CPython (with deterministic reference counting)

lines = filter(None, (line.rstrip() for line in open(filename)))

In Python 2 use itertools.ifilter if you want a generator and in Python 3, just pass the whole thing to list if you want a list.

How to delete all blank lines in the file with the help of python?

import fileinput
for line in fileinput.FileInput("file",inplace=1):
if line.rstrip():
print line

Eliminate blank lines in file read Python

Your file has a new line character at the end of each line like:

company-lab\n
company-bill\n
company-stage\n
company-dlab\n
company-nonprod\n
company-prod\n
company-eng-cis # not here though this has an EOF (end-of-file) character.

So your call to print(line) is including these in the print! You can avoid this like:

aws_env_list="../../../source_files/aws_environments/aws_environments_all.txt"
with open(aws_env_list, 'r') as aws_envs:
for line in aws_envs.readlines():
print(line.strip()) # just strip the \n away!

UPDATE

If you would like to compute with just the text and not the newline character you can strip it away like this:

aws_env_list="../../../source_files/aws_environments/aws_environments_all.txt"
with open(aws_env_list, 'r') as aws_envs:
for line in aws_envs.readlines():
line = line.strip() # You can strip it here and reassign it to the same variable
# Now all your previous code with the variable 'line' will work as expected
print(line) # no need to strip again
do_computations(line) # you can pass it to functions without worry

Delete blank/empty lines in text file for Python [closed]

If you want to remove blank lines from an existing file without writing to a new one, you can open the same file again in read-write mode (denoted by 'r+') for writing. Truncate the file at the write position after the writing is done:

with open('file.txt') as reader, open('file.txt', 'r+') as writer:
for line in reader:
if line.strip():
writer.write(line)
writer.truncate()

Demo: https://repl.it/@blhsing/KaleidoscopicDarkredPhp

how to skip over lines of a file if they are empty

Your problem comes from the fact that empty lines are not None, as you seem to assume. The following is a possible fix:

for line in ifile:
line = line.strip()
if not line: # line is blank
continue
if line.startswith("#"): # comment line
continue
data = line.split(',')
# do stuff with data


Related Topics



Leave a reply



Submit