What is the most pythonic way of reading in a named file, lines of lines that are either empty, contain only spaces, or have # as the first character, and then process the remaining lines? Suppose all of this fits easily into memory.
Note: this is not difficult to do - what I ask is the most pythonic way. I wrote a lot of Ruby and Java and have lost the feeling.
Here's the straw:
file_lines = [line.strip() for line in open(config_file, 'r').readlines() if len(line.strip()) > 0]
for line in file_lines:
if line[0] == '#':
continue
# Do whatever with line here.
I'm interested in nodule, but not at the cost of reading difficulties.
source
share