How to remove empty lines with re?

Bror Johansson bror.johansson at saabtech.se
Fri Oct 10 10:01:19 EDT 2003


"ted" <tedNOSPAM94107 at yahoo.com> wrote in message
news:vocoudjtp6vv25 at corp.supernews.com...
> I'm having trouble using the re module to remove empty lines in a file.
>
> Here's what I thought would work, but it doesn't:
>
> import re
> f = open("old_site/index.html")
> for line in f:
>     line = re.sub(r'^\s+$|\n', '', line)
>     print line
>

nonempty = [x for x in f if not x.strip()]

/BJ






More information about the Python-list mailing list