text wrapping help
attn.steven.kuo at gmail.com
attn.steven.kuo at gmail.com
Wed Feb 28 20:27:57 EST 2007
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