pythonian way
Fredrik Lundh
effbot at telia.com
Fri Feb 25 04:43:07 EST 2000
Alexander Williams <thantos at chancel.org> wrote:
> >Yeah, but I need to remove empty lines as well...
>
> Try:
>
> >>> A = filter(None, H.readlines())
>
> filter() with None as the function to use as the test for elements
> removes any that test as False intrinsically; since the empty line is
> always False, it returns a list with only strings with length. Bingo.
not really.
readlines returns an empty line as "\n", not as
an empty string. replacing None with a suitable
lambda expression might help:
A = filter(lambda s: len(s) > 1, H.readlines())
or (easier to read), ignore empty lines (and maybe
comments too?) in the processing loop. e.g.
for line in H.readlines():
if line[:1] in "#\n":
continue # skip this one
...
</F>
More information about the Python-list
mailing list