How to remove empty lines with re?
Klaus Alexander Seistrup
spam at magnetic-ink.dk
Fri Oct 10 13:16:28 EDT 2003
Anand Pillai wrote:
> Here is the complete code.
>
> import re
>
> empty=re.compile('^$')
> for line in open('test.txt').readlines():
> if empty.match(line):
> continue
> else:
> print line,
The .readlines() method retains any line terminators, and using the
builtin print will suffix an extra line terminator to every line,
thus effectively producing an empty line for every non-empty line.
You'd want to use e.g. sys.stdout.write() instead of print.
// Klaus
--
><> unselfish actions pay back better
More information about the Python-list
mailing list