給定一個隨機文本,我如何識別在點“。”之前有空格的句子,并洗掉這個空格?
my_text = 'The pressure is already being monitored. We will report the results soon . kind regards.'
預期結果(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.'
uj5u.com熱心網友回復:
import re
my_text = 'The pressure is already being monitored. We will report the results soon . kind regards.'
res = re.sub(r'\s \.', r'.', my_text)
print(res)
uj5u.com熱心網友回復:
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/478130.html
