[Tutor] Pythonic way to normalize vertical whitespace

spir denis.spir at free.fr
Sat May 9 12:51:49 CEST 2009


Le Fri, 08 May 2009 13:03:47 -0400,
python at bdurham.com s'exprima ainsi:

[...]
> Approaches:
> 1. split text to list of lines that get stripped then:
> a. walk this list building a new list of lines that track and
> ignore extra blank lines
> -OR-
> b. re-join lines and replace '\n\n\n' wth' \n\n' until no more
> '\n\n\n' matches exist
> 2. use regular expressions to match and replace whitespace
> pattern of 3 or more adjacent \n's with surrounding whitespace
> 3. a 3rd party text processing library designed for efficiently
> cleaning up text
> Thanks!

You should try 1a and 1b if only to have written the code at least once ;-) The set of python string methods is very complete and practicle.
Still, in this case, 2 is rather straightforward and simple. If you cannot figure out the proper pattern, have a look at http://www.amk.ca/python/howto/regex/ and/or ask again the list. 

Denis
------
la vita e estrany


More information about the Tutor mailing list