Ignoring comments - Parsing input file

Yigal Duppen yduppen at xs4all.nl
Sat Oct 12 05:32:12 EDT 2002


Simon Faulkner wrote:

> I am writing a script which reads it's input from a text file
> 
> What is the easiest way to make it ignore lines that start with # or
> blank lines?

# python2.2 code
# untested :-)
for line in inputfile:
        if not line.strip():
                # blank line, ignore
                continue

        if line.startswith("#"):
                # comment line, ignore
                continue

        # line is not empty, not a comment
        process_line(line)


Hope this helps,
YDD
-- 
http://www.xs4all.nl/~yduppen



More information about the Python-list mailing list