簡體   English   中英

Python Regex將txt文件分成段落

[英]Python Regex separating txt files into paragraphs

有沒有辦法使用正則表達式將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,但是如何決定只在正則表達式中查找換行符而不在空格中查找呢?

謝謝您的幫助。

不要使用正則表達式,只需使用split即可:

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

注意:

  • 我使用strip刪除任何前導/尾隨空格(包括Windows上的\\ r)
  • 無需使用re.split,因為您可以分割為簡單字符

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM