Something keeps nibbling on my list

michael mhyoung at valdosta.edu
Wed Apr 18 17:03:07 EDT 2001


Excluding lines that begin with '#' is exactly what I intended to use it for
but I took out the # for something else and forgot about it when i copied the
program to the newsgroup.
thx for the program. looks much better than mine. Im new to Python and am
using this to help me learn. Do you know why the first couple of lines would
be cut off like that??

      Michael



Steve Purcell wrote:

> michael wrote:
> > EXCLUDE=''   #Lines beginning with this should not be printed.
> >
> > def get_line() :
> >   buf = f.readline()
> >   if (buf and buf[0] == EXCLUDE):
> >     return 0
> >   else:
> >     return buf
>
> If 'EXCLUDE' is '', buf[0] will never == EXCLUDE.
>
> Imagine that you want to exclude lines beginning with '#'. I think you
> could then write your whole program as follows:
>
>     FILENAME = '/etc/hosts.deny'
>     OUTPUT = 'hosts.deny.test'
>
>     orig = open(FILENAME)
>     lines = orig.readlines()
>     orig.close()
>
>     def skip(line):
>        return line[:1] == '#'
>
>     filtered = open(OUTPUT,'w')
>     for linenum in range(len(lines)):
>        line = lines[linenum]
>        if skip(line):
>            continue
>        if line in lines[:linenum]:   # duplicate
>            continue
>        filtered.write(line)
>
>     filtered.close()
>
> -Steve
>
> --
> Steve Purcell, Pythangelist
> Get testing at http://pyunit.sourceforge.net/
> Any opinions expressed herein are my own and not necessarily those of Yahoo
>
> --
> http://mail.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list