textwrap.wrap() breaks non-breaking spaces

Ned Batchelder ned at nedbatchelder.com
Sun May 17 16:24:24 EDT 2015


On Sunday, May 17, 2015 at 3:40:07 PM UTC-4, Johannes Bauer wrote:
> Hey there,
> 
> so that textwrap.wrap() breks non-breaking spaces, is this a bug or
> intended behavior? For example:
> 
> Python 3.4.0 (default, Apr 11 2014, 13:05:11)
> [GCC 4.8.2] on linux
> 
> >>> import textwrap
> >>> for line in textwrap.wrap("foo dont\xa0break " * 20): print(line)
> ...
> foo dont break foo dont break foo dont break foo dont break foo dont
> break foo dont break foo dont break foo dont break foo dont break foo
> dont break foo dont break foo dont break foo dont break foo dont break
> foo dont break foo dont break foo dont break foo dont break foo dont
> break foo dont break
> 
> Apparently it does recognize that \xa0 is a kind of space, but it thinks
> it can break any space. The point of \xa0 being exactly to avoid this
> kind of thing.

There's a Python bug about this: http://bugs.python.org/issue20491

--Ned.

> 
> Any remedy or ideas?
> 
> Cheers,
> Johannes



More information about the Python-list mailing list