Reputation: 3425
So I have some sentences like:
The window is over there. The lamp is on. The fire is burning.
When I split it using split('.') and then join it with newlines, it loses the "."
Then I tried regex like (?<=\.)\s
but it produces a space before the first letter of the second and third letters:
The window is over there.
The lamp is on.
The fire is burning.
I don't want that extra space. I want:
The window is over there.
The lamp is on.
The fire is burning.
Thanks
Upvotes: 0
Views: 175
Reputation: 5338
There are several ways of dealing with splitting your input: stripping after splitting, using a regular expression to split or using a simple search .
The first option is perhaps the most intuitive: you split the string on a dot like you already do, and then strip the resulting strings to remove any whitespace and restore the trailing dot. In Python:
sentences = input.split('.')
sentences = [s.strip() + '.' for s in sentences if s]
print sentences.join('\n')
A second and simpler approach is to simple replace '. ' with '.\n':
print input.replace('. ', '.\n')
This will work with your input, but will fail if someone uses two spaces to separate sentences (which some people prefer).
The final and most flexible approach is to use a regular expression to split on the combination of a dot and whitespace:
import re
sentences = re.split('(?<=\.)\s*', input)
print sentences.join('\n')
Notice the important difference with your regular expression: I used \s* to consume all possible whitespace. This matters in cases where there are two or more spaces, or none at all.
Upvotes: 1
Reputation: 21378
Obviously not dealing with special cases (i.e. no space after a period), why not just do:
>>> s = 'The window is over there. The lamp is on. The fire is burning.'
>>> print s.replace('. ', '.\n')
The window is over there.
The lamp is on.
The fire is burning.
Upvotes: 1
Reputation:
>>> test = "The window is over there. The lamp is on. The fire is burning."
>>> print test.replace(". ",".\n")
The window is over there.
The lamp is on.
The fire is burning.
Upvotes: 3