Reputation: 6197
I would like to split up a text after a certain number of non-space and non-paragraph characters.
So far, I know that you can do this to split up a string after a total number of characters
cutOff = 10
splitString = oldString[0:cutOff]
But how do I do this so that it does not factor spaces in the character count?
Upvotes: 0
Views: 52
Reputation: 23089
You can use a regular expression. This returns a two-element tuple (list) containing the two halves of the input string broken at the desired location:
import re
data = """Now is the time
for all good men
to come"""
def break_at_ignoring_whitespace(str, break_at):
m = re.match(r"((\s*\w){%d})(.*)" % break_at, str, re.S)
return (m.group(1), m.group(3)) if m else (str, '')
r = break_at_ignoring_whitespace(data, 14)
print(">>" + r[0] + "<<")
print(">>" + r[1] + "<<")
Result:
>>Now is the time
fo<<
>>r all good men
to come<<
Upvotes: 1
Reputation: 11496
You can do a while
loop.
oldString = "Hello world"
cutOff = 10
i = 0
while i < cutOff and cutOff < len(oldString):
if oldString[i] in [' ', '\n']: cutOff += 1
i += 1
splitString = oldString[:cutOff]
Upvotes: 1