Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python正则表达式将txt文件分隔为段落_Python_Regex_Text - Fatal编程技术网

Python正则表达式将txt文件分隔为段落

Python正则表达式将txt文件分隔为段落,python,regex,text,Python,Regex,Text,有没有一种方法可以使用正则表达式将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 a

有没有一种方法可以使用正则表达式将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,但是如何决定只在regex中查找换行符而不查找空格


谢谢您的帮助。

不要使用regexp,只需使用split:

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

  • 我使用strip删除任何前导/尾随空格(包括windows上的空格)
  • 没有必要使用re.split,因为您可以在一个简单的字符上进行拆分