How to find the best solution ?

J

Johny

I have a text and would like to split the text into smaller parts,
say into 100 characters each. But if the 100th character is not a
blank ( but word) this must be less than 100 character.That means the
word itself can not be split.
These smaller parts must contains only whole( not split) words.
I was thinking about RegEx but do not know how to find the correct
Regular Expression.
Can anyone help?
Thanks
L.
 
T

Tim Golden

I have a text and would like to split the text into smaller parts,
say into 100 characters each. But if the 100th character is not a
blank ( but word) this must be less than 100 character.That means the
word itself can not be split.
These smaller parts must contains only whole( not split) words.
I was thinking about RegEx but do not know how to find the correct
Regular Expression.
Can anyone help?
Thanks
L.

Have a look at the textwrap module

TJG
 
T

Tim Chase

Johny said:
I have a text and would like to split the text into smaller parts,
say into 100 characters each. But if the 100th character is not a
blank ( but word) this must be less than 100 character.That means the
word itself can not be split.
These smaller parts must contains only whole( not split) words.
I was thinking about RegEx but do not know how to find the correct
Regular Expression.

While I suspect you can come close with a regular expression:

import re, random
size = 100
r = re.compile(r'.{1,%i}\b' % size)
# generate a random text string with a mix of word-lengths
words = ['a', 'an', 'the', 'four', 'fives', 'sixsix']
data = ' '.join(random.choice(words) for _ in range(200))
# for each chunk of 100 characters (or fewer
# if on a word-boundary), do something
for bit in r.finditer(data):
chunk = bit.group(0)
print "%i: [%s]" % (len(chunk), chunk)

it may have an EOF fencepost error, so you might have to clean up
the last item. My simple test seemed to show it worked without
cleanup though.

-tkc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top