Wrapping long lines 
Author Message
 Wrapping long lines

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 + \

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 ---------------------------



Fri, 08 Apr 2005 07:56:00 GMT  
 Wrapping long lines

Quote:

> 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.

Nothing wrong with reinventing the wheel, it's good practice and teaches a
lot. But I'm sure you've heard of `fmt` and its ilk for this problem. For
fancier stuff where fmt isn't enough, pr typically can do the job.

$ fmt {*filter*}_file | less

is often all you need.

Cheers,

f.



Fri, 08 Apr 2005 08:06:13 GMT  
 Wrapping long lines


Quote:
> 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?

I believe that there is or will be a 'textwrap.py' (or something
close) module in the standard lib for 2.3.  You may find it
interesting to compare when you get a chance.

TJR



Fri, 08 Apr 2005 12:43:03 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. long line too long?

2. long lines, long string

3. Line wrapping

4. Line wrapping in listboxes?

5. Edit field line wrapping

6. Line Wrapping

7. Line-wrap test

8. disable line wrap in xterm??

9. How to do line wrapping?

10. Recognizing wrapped lines

11. line wrapping in news messages

12. Scrolling wrapped lines

 

 
Powered by phpBB® Forum Software