Python 计数段落

Python Count paragraph

大家好,我的任务是统计行数和段落数。计算每一行显然很容易,但我坚持计算段落。如果一个段落没有字符,它将返回数字零,并且每个段落的增量都更高。例如输入文件是:Input and an Output should come out Output 所以我的代码是:

def insert_line_para_nums(infile, outfile):
    f = open(infile, 'r')
    out = open(outfile, 'w')
    linecount = 0
        for i in f:
            paragraphcount = 0
            if '\n' in i:
                linecount += 1
            if len(i) < 2: paragraphcount *= 0
            elif len(i) > 2: paragraphcount = paragraphcount + 1
            out.write('%-4d %4d %s' % (paragraphcount, linecount, i))
    f.close()
    out.close()
def insert_line_para_nums(infile, outfile):
    f = open(infile, 'r')
    out = open(outfile, 'w')
    linecount = 0
    paragraphcount = 0
    empty = True
    for i in f:
        if '\n' in i:
            linecount += 1
            if len(i) < 2:
                empty = True
            elif len(i) > 2 and empty is True:
                paragraphcount = paragraphcount + 1
                empty = False
            if empty is True:
                paragraphnumber = 0
            else:
                paragraphnumber = paragraphcount
        out.write('%-4d %4d %s' % (paragraphnumber, linecount, i))
    f.close()
    out.close()

这是一种方法,但不是最漂亮的方法。

import re
f = open('a.txt', 'r')

paragraph = 0

lines = f.readlines()

for idx, line in enumerate(lines):
    if not line == '\n':
        m = re.search(r'\w', line)
        str = m.group(0)

    try:
        # if the line is a newline, and the previous line has a str in it, then  
        # count it as a paragraph.
        if line == '\n' and str in lines[idx-1]: 
            paragraph +=1
    except:
        pass

if lines[-1] != '\n': # if the last line is not a new line, count a paragraph.
    paragraph +=1

print paragraph