考虑我有一个字符串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获取段落数?
答案 0 :(得分:2)
你似乎有两个'\ n'作为段落分隔符。在这种情况下,段落的数量是:
text.count('\n\n') + 1
答案 1 :(得分:0)
如果您的段落以单个换行符或双换行符结尾,则可以将整个文本拆分为' \ n'然后跳过用空文本计算任何行(表示空的新行)。
例如,
lines = text.split('\n')
count = 0
if not line.strip() == '':
count += 1