[Tutor] Pythonic way to normalize vertical whitespace

bob gailer bgailer at gmail.com
Fri May 8 22:43:02 CEST 2009


python at bdurham.com wrote:
> Note: Following cross-posted to python-list where it got queued due to 
> suspicious subject line.
>
> I'm looking for suggestions on technique (not necessarily code) about 
> the most pythonic way to normalize vertical whitespace in blocks of 
> text so that there is never more than 1 blank line between paragraphs. 
> Our source text has newlines normalized to single newlines (\n vs. 
> combinations of \r and \n), but there may be leading and trailing 
> whitespace around each newline.

I can't follow that! Please provide a before and after example.

>
> 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!
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>   


-- 
Bob Gailer
Chapel Hill NC
919-636-4239


More information about the Tutor mailing list