do replacement evenly

python at python at
Tue Jun 2 10:44:04 EDT 2009

Here's how we normalize whitespace in multiline blocks of text. Perhaps
you can adapt this routine to your needs? Watch for line wrap.

# clean up duplicate whitespace, leading/trailing whitespace, triple
def fixwhitespace( text ):
	output = []
	lastLine = ''
	# split text into list of individual lines
	lines = text.strip().splitlines()
	for line in lines:
		# remove leading, trailing, and duplicate whitespace within a line
		line = ' '.join( line.split( None ) )
		# ignore multiple blank lines
		if not line and not lastLine:
			output.append( line )
		lastLine = line
	return '\n'.join( output )


More information about the Python-list mailing list