0

有没有办法使用正则表达式将 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 但是你如何决定只在正则表达式中寻找换行符而不是空格?

谢谢您的帮助。

4

1 回答 1

1

不要使用正则表达式,只需使用拆分:

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

注意:

  • 我使用 strip 删除任何前导/尾随空格(包括 Windows 上的 \r )
  • 无需使用 re.split,因为您可以拆分一个简单的字符
于 2013-07-09T11:37:34.840 回答