2013-01-23 40 views
0

在轉換單詞doc中的文本時,原作者使用了一行句點
Friends and family......................................-1
XBox 360 ................................................-2

我想擺脫連續句點的長行。我想保留那些使用時期完整的句子。所以我不想刪除所有時段,只是當它們出現在3個或更多的組中時。從字符串中刪除連續的點(句點)?

回答

3

使用正則表達式:

import re 

consequitivedots = re.compile(r'\.{3,}') 
consequitivedots.sub('', inputstring) 

示範:

>>> import re 
>>> consequitivedots = re.compile(r'\.{3,}') 
>>> inputstring = '''\ 
... Friends and family......................................-1 
... XBox 360 ................................................-2 
... ''' 
>>> consequitivedots.sub('', inputstring) 
'Friends and family-1\nXBox 360 -2\n' 
>>> print consequitivedots.sub('', inputstring) 
Friends and family-1 
XBox 360 -2 
+0

謝謝!這顯然工作得很好。 – Johnston

相關問題