Chris,
Thank you for your reply.
So, let's suppose the example:
S='this is just a random sequence of letters courtesy of monkeys on
typewriter.'
and the length of split strings is now 10( instead of 6)
The example should be like this
this is
just a
random
sequence
of letters
courtesy
of monkeys
on
typewriter
In other words, the string should be split at every 10th possition but
if the 10th character is space, then the string must be split at the
nearest space before the 10th possition.
It could be better explained if the length of split strings will be
20.
S='this is just a random sequence of letters courtesy of monkeys on
Results:
this is just a
random sequence of
letters courtesy of
monkeys on
typewriter.'
Any idea how to do that?
Thank you
L.
To be honest I couldn't think of a neat way of doing it.
What seems to look right, albeit fairly ugleh was this.
import string
def digest_chunks(input_string, max_length):
tmp = []
while input_string:
if len(input_string) >= max_length:
if input_string[max_length] not in string.whitespace:
y = max_length - input_string[max_length::-1].find('
')
if not y:
if len(input_string) < max_length:
tmp.append(input_string)
input_string = ''
else:
y = input_string[::-1].find(' ', 1)
tmp.append(input_string[:y])
input_string = input_string[y:]
else:
tmp.append(input_string[:y])
input_string = input_string[y:]
else:
tmp.append(input_string[:max_length])
input_string = input_string[max_length:]
else:
tmp.append(input_string)
input_string = ''
return '\n'.join(tmp)
s = 'this is just a random sequence of letters courtesy of monkeys on
typewriters.'
print digest_chunks(s, 20)
this is just a
random sequence of
letters courtesy of
monkeys on
typewriters.