text wrapping help

Ryan K ryankaskel at gmail.com
Wed Feb 28 20:50:06 EST 2007


That works great but I need to replace the newlines with 24-(the index
of the \n) spaces.



On Feb 28, 8:27 pm, attn.steven.... at gmail.com wrote:
> On Feb 28, 4:06 pm, "Ryan K" <ryankas... at gmail.com> wrote:
>
> > I'm trying to text wrap a string but not using the textwrap module. I
> > have 24x9 "matrix"  and the string needs to be text wrapped according
> > to those dimensions. Is there a known algorithm for this? Maybe some
> > kind of regular expression? I'm having difficulty programming the
> > algorithm.
>
> Try:
>
> import re
> sample_text = """Personal firewall software may warn about the
> connection IDLE makes to its subprocess using this computer's internal
> loopback interface.  This connection is not visible on any external
> interface and no data is sent to or received from the Internet."""
>
> # assume 24 is sufficiently wide:
>
> lines = map(lambda x: x.rstrip(),
> re.findall(r'.{1,24}(?:(?<=\S)\s|$)', sample_text.replace("\n", " ")))
>
> print "\n".join(lines)
>
> --
> Hope this helps,
> Steven





More information about the Python-list mailing list