2013-07-09 96 views
0

有沒有一種方法可以使用正則表達式來將txt文件分成段落?Python正則表達式將txt文件分成段落

舉個例子,如果這是我的文本文件:

They call for you: The general who became a slave; the slave who became a gladiator; the gladiator who defied an  Emperor. Striking story. 

Watch your thoughts, for they will become actions. Watch your actions, for they'll become...habits. Watch your habits for they will forge your character. Watch your character, for it will make your destiny. 

我會想到的是,\ S這裏所涉及的,但如何你決定只尋找在正則表達式換行,而不是空白?

感謝您的幫助。

回答

1

不要使用正則表達式,只需使用分裂:

a="""1.... 
2.... 
3....""" 
print map(lambda x:x.strip(),a.split("\n")) 

NB:

  • 我使用條以除去任何前/後空間(包括在Windows \ R)
  • 沒有必要使用re.split,因爲您可以分割成簡單的字符
相關問題