To re or not to re ... ( word wrap function?)

Magnus Lie Hetland mlh at idi.ntnu.no
Sat Sep 22 11:12:05 EDT 2001


As usual, even though lots of code has been supplied
I just have to add my version <wink>

A small (naïve) word-wrapper I wrote a long time ago
(changed to use string methods):

-----------------------------------------------------

import fileinput

limit, current, inside = 80, -1, 0

for line in fileinput.input():
    words = line.split()
    if words:
        inside = 1
        for word in words:
            increment = 1 + len(word)
            if current + increment > limit:
                current = increment-1
                print
            else:
                current = current+increment
                print word,
    else:
        if inside:
            inside = 0
            print
        print # Indent this to `squeeze' the empty lines
        current = -1

if inside: print

-----------------------------------------------------

--

  Magnus Lie Hetland         http://www.hetland.org

 "Reality is that which, when you stop believing in
  it, doesn't go away."           -- Philip K. Dick






More information about the Python-list mailing list