portable fortune program

Alex Martelli aleax at aleax.it
Mon Apr 29 12:08:59 EDT 2002


J.Jacob wrote:
        ...
>     quote = ''
>     if not quoteslist:
>         for s in quotesfile.readlines():
>             if s[0] == '%':
>                 if quote:
>                     quoteslist.append(quote)
>                     quote = ''
>             else: quote = quote + s
>         if quote: quoteslist.append(quote)

It appears to me that:

    quoteslist = quotesfile.read().split('\n%')

should be roughly equivalent to this -- except
for removing the trailing newline of each item
of quoteslist; some variation on this split,
depending on what else you want to have on
those lines starting with %, perhaps with a
following cleanup step if those lines starting
with % have to allow arbitrary comments to be
removed, might in any case be faster & handier,
as long as you're building the quoteslist in
memory anyway.


Alex




More information about the Python-list mailing list