text wrapping help

attn.steven.kuo at gmail.com attn.steven.kuo at gmail.com
Thu Mar 1 03:04:23 CET 2007


On Feb 28, 5:50 pm, "Ryan K" <ryankas... at gmail.com> wrote:

> On Feb 28, 8:27 pm, attn.steven.... at gmail.com wrote:
>
> > 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
>
> That works great but I need to replace the newlines with 24-(the index
> of the \n) spaces.
>



Just left-justify to the appropriate width with
the the padding character you wanted:

equally_long_lines = map(lambda x: x.ljust(24, ' '), lines)

print "\n".join(equally_long_lines)

--
Hope this helps,
Steven




More information about the Python-list mailing list