Skip to content


Python: strip whitespace/comments when reading a file

From StackOverflow

def stripped(f):
    for l in f:
        line = l.rstrip()
        if line and not line.startswith("#"):
            yield line

with(open "foo.txt", "r") as f:
    for line in stripped(f):
        print line

Posted in Uncategorized.


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.