Wrapping long lines

Will Stuyvesant hwlgw at hotmail.com
Sun Oct 20 19:56:00 EDT 2002


Sometimes they send me a file, for me to read, and it has *very* long
lines.  Their editor--from--hell just puts a whole paragraph into one
line!  Recognize the problem?

So here is yet another python newbie script for yet another silly
problem.  And the usual questions: Comments?  Improvements?  Would
list comprehension or map/reduce/filter combos be better?

At first I thought 'hey I can use the re module?', but then I had more
problems than I started with.


'''
I've been playing spoilsport in an attempt to get tabnanny.py working,
but now that there's absolutely no reason to continue with this, the
amount of my life I'm willing to devote to it is unbounded <0.9 wink>.

    -- Tim Peters, 30 Mar 1998
'''

--------------- cut here for maxlinelen.py ---------------------------

usage = '''
Usage:
maxlinelen.py <filename> <len>

Breaks any line in filename that is over len characters long into
lines with a maximum length of len.  Tries to break a line at suitable
characters, see the delimstr variable.
'''

import sys
import string

delimstr = string.whitespace + \
    r'`¦!"£$%^&*()_-+={}[];:@#~\|,<.>/?€' + "'"

def toMaxLen(line, maxlen):
    r''' Break a line in smaller lines.  Try breaking a line at
    a character in delimstr.  Recursive algorithm.

    >>> toMaxLen('12345', 66)
    '12345'
    >>> toMaxLen('12345', 3)
    '123\n45'
    >>> toMaxLen('1234567890', 3)
    '123\n456\n789\n0'
    >>> toMaxLen('12 456 8901', 3)
    '12 \n456\n890\n1'
    '''
    if len(line) > maxlen:
        index = 0
        spos = -1
        while index < maxlen-1:
            if line[index] in delimstr:
                spos = index
            index = index + 1
        if spos < 1:                # no nice place to break the line
            spos = maxlen
        start = line[:spos]
        endstr = line[spos:]
        if (spos < maxlen) and (endstr[0] == ' '):
            endstr = endstr[1:]     # no leading space on a line!
        end = toMaxLen(endstr, maxlen)
        line = start + '\n' + end
    return line

def _test():
    import doctest, maxlinelen
    return doctest.testmod(maxlinelen)

def run():
    if len(sys.argv) < 3:
        print usage
        sys.exit()
    else:
        f = open(sys.argv[1], 'r')
        maxlen = int(sys.argv[2])
        lines = []
        for line in f:
            if len(line) > maxlen:
                line = toMaxLen(line, maxlen)
            lines.append(line)
        f.close()
        f = open(sys.argv[1], 'w')
        f.writelines(lines)
        f.close()


if __name__ == '__main__':
    #_test()
    run()
--------------- cut here for maxlinelen.py ---------------------------



More information about the Python-list mailing list