How to remove empty lines with re?

Anand Pillai pythonguy at Hotpop.com
Fri Oct 10 12:50:55 EDT 2003


Errata:

I meant "there is end just after the beginning" of course.

-Anand

"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
> 
> Also, when I try to remove some HTML tags, I get even more empty lines:
> 
> import re
> f = open("old_site/index.html")
> for line in f:
>     line = re.sub('<.*?>', '', line)
>     line = re.sub(r'^\s+$|\n', '', line)
>     print line
> 
> I don't know what I'm doing. Any help appreciated.
> 
> TIA,
> Ted




More information about the Python-list mailing list