Something keeps nibbling on my list
Steve Purcell
stephen_purcell at yahoo.com
Tue Apr 17 16:00:05 EDT 2001
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
More information about the Python-list
mailing list