'How can I identify sentences that have a space before a dot '.' and remove this space
Given a random text, how can I identify sentences that have a space before a dot '.', and remove this space?
my_text = 'The pressure is already being monitored. We will report the results soon . kind regards.'
Expected result (We will report the results soon . => We will report the results soon.):
my_text = 'The pressure is already being monitored. We will report the results soon. kind regards.'
Solution 1:[1]
k=[] # Take an empty list
for x in my_text.split('.'): # Split with all the '.'
if x==0: # For the First iteration do not add '.'
k.append(x.rstrip()) # rstrip() will remove any number of extra spaces
else:
k.append(x.rstrip()+'.') # Add '.' after each sentence.
''.join(k[:-1]) # Removing the last '.' since it will be inserted twice
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
