Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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中查找字符串中没有段落_Python_Regex_Python 2.7 - Fatal编程技术网

如何在python中查找字符串中没有段落

如何在python中查找字符串中没有段落,python,regex,python-2.7,Python,Regex,Python 2.7,假设我有一个字符串saysample,它是 "ApacheBench (ab) is a benchmarking tool that can load test servers by sending an arbitrary number of concurrent requests. It was designed for testing Apache installations. cURL is a command line tool to send and receive data u

假设我有一个字符串say
sample
,它是

"ApacheBench (ab) is a benchmarking tool that can load test servers by sending an arbitrary number of concurrent requests. It was designed for testing Apache installations.

cURL is a command line tool to send and receive data using various protocols like  HTTP, HTTPS, FTP, LDAP etc. It comes by default with most of the Linux distributions. 

Wrk is a tool that is similar to traditional Apache Benchmark. Configuration and execution are through a command line tool. It has very few but powerful settings, only the essential to generate HTTP load."

上面的字符串中有3个段落。如何使用python获取段落数?

您似乎有双'\n'作为段落分隔符。在这种情况下,段落数为:

text.count('\n\n') + 1

您似乎有两个'\n'作为段落分隔符。在这种情况下,段落数为:

text.count('\n\n') + 1

如果段落以单换行符或双换行符结尾,则可以将整个文本拆分为“\n”,然后跳过计算任何空文本行(表示新的空行)

比如说,

lines = text.split('\n')
count = 0
if not line.strip() == '':
    count += 1

如果段落以单换行符或双换行符结尾,则可以将整个文本拆分为“\n”,然后跳过计算任何空文本行(表示新的空行)

比如说,

lines = text.split('\n')
count = 0
if not line.strip() == '':
    count += 1

你试过数数换行符吗?看看article@MauroBaraldi当前位置指向OP肯定有点过头了。除此之外,我希望您注意到这篇文章来自Python 2.1时代(大约有20年历史),对于一个需要任意分隔符的文本文件,生成器可以在大约6行代码中完成这项工作。如果您的段落以
\n[空格]\n
结尾,那么只需创建一个正则表达式来查找该标记,然后数一数文本中有多少个换行符。你试过数一数换行符吗?看看article@MauroBaraldi当前位置指向OP肯定有点过头了。除此之外,我希望您注意到这篇文章来自Python 2.1时代(大约有20年历史),对于一个需要任意分隔符的文本文件,生成器可以在大约6行代码中完成这项工作。如果您的段落以
\n[空格]\n
结尾,那么只需创建一个正则表达式来查找该标记,数一数文本中有多少行。如果行尾是CRLF怎么办?还是混合?@BartvanNierop:在执行任何其他操作之前,将所有
\r\n
转换为
\n
。如果数据包含三行或三行以上的空白行,这可能会产生意外结果。如果行尾是CRLF怎么办?还是混合?@BartvanNierop:在执行任何其他操作之前,将所有
\r\n
转换为
\n
。如果数据包含三行或更多空行,则可能会产生意外结果。