Remove empty lines

Sheila King usenet at thinkspot.net
Wed Mar 20 14:52:21 EST 2002


On Wed, 20 Mar 2002 10:33:55 -0800, "Nick Arnett" <narnett at mccmedia.com>
wrote in comp.lang.python in article
<mailman.1016649277.6397.python-list at python.org>:

> The problem is here, I think.  Split in Python appears to return the
> separator, so the '\n' characters are still there.

After posting my last article, I went back and looked at this thread more
carefully, and saw that it was using the re module. So, here is an example,
also, using re:

>>> mystring = 'Hello.\n\nHow are you today?\nFine, thanks.\n\n\nBye.'
>>> re.split('\n',mystring)
['Hello.', '', 'How are you today?', 'Fine, thanks.', '', '', 'Bye.']
>>> filter(None, re.split('\n', mystring))
['Hello.', 'How are you today?', 'Fine, thanks.', 'Bye.']
>>> for line in filter(None, re.split('\n', mystring)):
	print line

	
Hello.
How are you today?
Fine, thanks.
Bye.
>>> 

By the way, I wonder that it is even necessary to trot out the re module
for something as simple as removing comments and empty lines from code. You
should be able to do this with just string methods, and it should be faster
that way.

What is the characteristics to determine a comment in Java? (I haven't
coded in Java, so I'm not certain...I could make guesses, but...)

Would it by any chance be similar to comments in C++?

comment starts with

//

and all following characters on the same line,

Or enclosed within

/*  */


Something this simple shouldn't require re.

--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/




More information about the Python-list mailing list