A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://mail.python.org/pipermail/python-list/2001-April/097154.html below:

Something keeps nibbling on my list

Something keeps nibbling on my list Something keeps nibbling on my listSteve 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

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4