[issue1859] textwrap doesn't linebreak on "\n"

Serhiy Storchaka report at bugs.python.org
Sat Aug 4 17:26:37 CEST 2012


Serhiy Storchaka added the comment:

I believe that the method of work with newlines is too application specific.

Someone may prefer empty line separated paragraphs, here is another recipe:

def wrap_paragraphs(text, width=70, **kwargs):
    return [line for para in re.split(r'\n\s*\n', text) for line in (textwrap.wrap(para, width, **kwargs) + [''])][:-1]

And here is another application-specific recipe:

def format_html_paragraphs(text, width=70, **kwargs):
    return ''.join('<p>%s</p>' % '<br>'.join(textwrap.wrap(html.escape(para), width, **kwargs)) para in re.split(r'\n\s*\n', text))

I don't see a one obvious way to solve this problem, so I suggest a recipe, not a patch. In any case the specialized text-processing applications are not likely to use textwrap because most output now uses non-monowidth fonts. Textwrap is only for the simplest applications.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue1859>
_______________________________________


More information about the Python-bugs-list mailing list