2016-11-11 107 views
0

字符串认为我有一个字符串说sample这是如何找到无段落Python中

"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个段落在上面string.How我可以使用python的段落数?

+1

你试过计数换行符? – khelwood

+0

看看[这个](https://www.safaribooksonline.com/library/view/python-cookbook/0596001673/ch04s09.html)文章 –

+1

@MauroBaraldi:指点OP肯定是矫枉过正。除此之外,我希望你已经注意到这篇文章来自Python 2.1时代(〜20岁) - 对于一个需要任意分隔符的文本文件,一个生成器可以完成约6行的工作码。 – jsbueno

回答

2

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

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

如果行尾是CRLF,该怎么办?还是混合? –

+0

@BartvanNierop:在做任何事情之前,将所有的'\ r \ n'转换为'\ n'。 –

+0

如果数据包含三个或更多空白行的跨度,这可能会产生意想不到的结果。 –

0

如果你的段落有一个换行符或双线路中断结束后,你可以通过“\ n”分裂整个文本,然后跳到空计数的任何行文本(表示空白的新行)。

例如,

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