Ignoring comments - Parsing input file

Bengt Richter bokr at oz.net
Sun Oct 13 00:31:08 CEST 2002


On Sat, 12 Oct 2002 11:32:12 +0200, Yigal Duppen <yduppen at xs4all.nl> wrote:

>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?
>
I suspect Simon would want to count '      # blah ... ' as "starting with #" too,
so, (untested ;-)
># python2.2 code
># untested :-)
>for line in inputfile:
         sline = line.strip()
         if not sline or sline.startswith('#'): continue
>        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,
Ditto

Regards,
Bengt Richter



More information about the Python-list mailing list