ray at commsecure.com.au
Sun Feb 27 10:21:46 CET 2000
Alan Daniels wrote:
> On Fri, 25 Feb 2000 08:33:15 +0100, the infinitely wise Milos Prudek
> (prudek at nembv.cz) spoke forth to us, saying...
> >The following excerpt works. It reads lines from config file. But I
> >would like to ask if it is 'right' in the Pythonian way.
> >while Line<>'':
> > if Line<>'' and Line<>'\n':
> > A.append(Line)
> > Line=H.readline()
> I myself would use:
> A = 
> for Line in H.readlines():
> if Line not in ("", "\n"):
the test for "" is redundant for both implementations.
For the first the "while" test catches this, and for the second
the readlines() routine terminates on "", i.e. end of file, and
hence does not return a list with "" components.
I personally don't see the point to spotting '\n', if a config
file is being scanned, you either can live with empty lines, or
you want to nuke "empty" lines, i.e. lines where string.strip would
If the latter is the case then:
H = sys.stdin
for i in H.readlines():
if string.strip(i): # works for the unreliable family of OS's from MS
or if speed is to be sought in preference to clarity:
More information about the Python-list