2016-11-11 59 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