[Tutor] problem with back slash
Alan Gauld
alan.gauld at yahoo.co.uk
Wed Feb 23 17:31:52 EST 2022
On 23/02/2022 19:37, Alex Kleider wrote:
> I've written myself a little utility that accepts a text file which
> might have very long lines and returns a file with the same text but
> (as much as possible) with the lines no longer than MAX_LEN
> characters. (I've chosen 70.)
> It seems to work except when the the source file contains back
> slashes! (Presence of a back slash appears to cause the program
> to go into an endless loop.)
> I've tried converting to raw strings but to no avail.
>
> Here's the code, followed by an example source file.
>
> '''
> #!/usr/bin/env python3
> # file: limit_line_length.py
> """
> Accepts a text file and tries to shorten lines to MAX characters.
> First parameter must be a file name.
> Optional second parameter can be the output file's name.
> If 2nd param is not specified, output will go to "new_<1stParam>".
> """
>
> import sys
>
> MAX = 70
>
>
> def split_on_space_closest_to_max_len(line, max_len=MAX):
> """
> Returns a tuple of two (possibly empty) strings.
> If the line is <= <max_len>: it is returned as t[0] & t[1] as ''.
> If indented beyond <max_len> t[0] as '' & t[1] as line[max_len:]
> If there are no spaces then t[0] as <line> and t[1] as ''.
> If the first space is beyond <max_len>: t[0] is what is up to the
> space and t[1] what was after the space.
> Otherwise t[0] is the longest it can be up to max_len up to a
> space and t[1] is what comes after the space.
> Trailing spaces are stripped.
> """
> line = line.rstrip()
> line_length = len(line)
> if line_length <= max_len: # simplest scenario
> return (line, '') # empty lines included
> original_line = line[:]
> unindented_line = line.lstrip()
> n_leading_spaces = line_length - len(unindented_line)
> if n_leading_spaces > max_len: # big indentation!!!
> return ('', line[max_len:])
> indentation = ' ' * n_leading_spaces
> max_len -= n_leading_spaces
> i_last_space = unindented_line.rfind(' ')
> if i_last_space == -1: # no spaces on which to split
> return (line, '')
> i_space = unindented_line.find(' ')
> if i_space > max_len:
> return (indentation + unindented_line[:i_space],
> unindented_line[i_space+1])
Missing colon on that line? See below for comparison...
> while True:
> next_space = unindented_line.find(' ', i_space+1)
> if next_space > max_len: break
> else: i_space =next_space
I think this loop could be replaced with a rfind()
on a slice upto max_space?
> return (indentation + unindented_line[:i_space],
> unindented_line[i_space +1:])
But I doubt that's the cause of the backslash issue.
And I have no idea what is. :-(
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list