Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #90785

Re: textwrap.wrap() breaks non-breaking spaces

Path csiph.com!usenet.pasdenom.info!news.albasani.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python.list@tim.thechases.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'string.': 0.05; 'class,': 0.07; 'expressions': 0.07; 'string': 0.09; 'spaces': 0.09; 'subject:skip:t 10': 0.09; 'tests,': 0.09; 'wrapper': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'bug': 0.12; '"hello': 0.16; '-tkc': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'notation,': 0.16; 're.compile(': 0.16; 'received:174.136': 0.16; 'splits': 0.16; 'splitting': 0.16; 'subclass': 0.16; 'subject:breaking': 0.16; 'subject:non': 0.16; 'textwrap': 0.16; 'wrote:': 0.18; 'hey': 0.18; 'split': 0.19; '>>>': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; "aren't": 0.24; 'recognize': 0.24; 'space.': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'source': 0.25; 'compare': 0.26; 'recognized': 0.26; 'header:In- Reply-To:1': 0.27; 'point': 0.28; 'words': 0.29; 'gives': 0.31; "skip:' 10": 0.31; 'apparently': 0.31; 'assert': 0.31; 'file': 0.32; 'class': 0.32; 'skip:m 30': 0.32; 'regular': 0.32; 'linux': 0.33; 'there,': 0.34; 'but': 0.35; 'there': 0.35; 'charset:us- ascii': 0.36; 'two': 0.37; 'received:10': 0.37; 'being': 0.38; 'e.g.': 0.38; 'that,': 0.38; 'little': 0.38; 'does': 0.39; 'break': 0.61; 'kind': 0.63; 'dont': 0.67; 'results': 0.69; 'skip:r 30': 0.69; 'skip:$ 10': 0.81; '2014,': 0.84
X-Sender-Id wwwh|x-authuser|tim@thechases.com
X-Sender-Id wwwh|x-authuser|tim@thechases.com
X-MC-Relay Neutral
X-MailChannels-SenderId wwwh|x-authuser|tim@thechases.com
X-MailChannels-Auth-Id wwwh
X-MC-Loop-Signature 1431893521139:1873795838
X-MC-Ingress-Time 1431893521139
Date Sun, 17 May 2015 15:12:36 -0500
From Tim Chase <python.list@tim.thechases.com>
To Johannes Bauer <dfnsonfsduifb@gmx.de>
Cc python-list@python.org
Subject Re: textwrap.wrap() breaks non-breaking spaces
In-Reply-To <mjaqqd$t0m$1@news.albasani.net>
References <mjaqqd$t0m$1@news.albasani.net>
X-Mailer Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu)
Mime-Version 1.0
Content-Type text/plain; charset=US-ASCII
Content-Transfer-Encoding 7bit
X-AuthUser tim@thechases.com
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.98.1431894065.17265.python-list@python.org> (permalink)
Lines 62
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1431894065 news.xs4all.nl 2936 [2001:888:2000:d::a6]:44012
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:90785

Show key headers only | View raw


On 2015-05-17 21:39, 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.
> 
> Any remedy or ideas?

Since it uses a TextWrapper class, you can subclass that and
then assert that the spaces found for splitting aren't
non-breaking spaces.  Note that, to use the "\u00a0"
notation, the particular string has to be a non-raw string.
You can compare the two regular expressions with those in
the original source file in your $STDLIB/textwrap.py

  import textwrap
  import re
  
  class MyWrapper(textwrap.TextWrapper):
    wordsep_re = re.compile(
      '((?!\u00a0)\\s+|'                      # any whitespace
      r'[^\s\w]*\w+[^0-9\W]-(?=\w+[^0-9\W])|' # hyphenated words
      r'(?<=[\w\!\"\'\&\.\,\?])-{2,}(?=\w))') # em-dash
  
    # This less funky little regex just split on recognized spaces. E.g.
    #   "Hello there -- you goof-ball, use the -b option!"
    # splits into
    #   Hello/ /there/ /--/ /you/ /goof-ball,/ /use/ /the/ /-b/ /option!/
    wordsep_simple_re = re.compile('((?!\u00a0)\\s+)')
  
  s = 'foo dont\u00a0break ' * 20
  
  wrapper = MyWrapper()
  for line in wrapper.wrap(s):
    print(line)

Based on my tests, it gives the results you were looking
for.

-tkc




Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

textwrap.wrap() breaks non-breaking spaces Johannes Bauer <dfnsonfsduifb@gmx.de> - 2015-05-17 21:39 +0200
  Re: textwrap.wrap() breaks non-breaking spaces Tim Chase <python.list@tim.thechases.com> - 2015-05-17 15:12 -0500
  Re: textwrap.wrap() breaks non-breaking spaces Ned Batchelder <ned@nedbatchelder.com> - 2015-05-17 13:24 -0700
  Re: textwrap.wrap() breaks non-breaking spaces Roy Smith <roy@panix.com> - 2015-05-21 22:36 -0400
    Re: textwrap.wrap() breaks non-breaking spaces wxjmfauth@gmail.com - 2015-05-22 06:51 -0700

csiph-web