Welcome, guest | Sign In | My Account | Store | Cart

I know there is the module textwrap and other recipes like http://code.activestate.com/recipes/148061-one-liner-word-wrap-function/

But in some constellations my recipe for a very simple word wrap might come in handy nevertheless.

Python, 8 lines
1
2
3
4
5
6
7
8
    #!/usr/bin/env python

    import re, sys

    width = 79

    for line in sys.stdin:
      sys.stdout.write(re.sub(r'(.{1,%d})(\s|$)+' % width, r'\1\n', line))