do replacement evenly

python at bdurham.com python at bdurham.com
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
CRLF's
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:
			pass
		else:
			output.append( line )
		lastLine = line
	
	return '\n'.join( output )

Regards,
Malcolm



More information about the Python-list mailing list