Python Count段落

时间:2015-02-21 19:42:22

标签: python paragraphs

大家好,所以我的任务是计算线条和段落。计算每一行显然很容易,但我坚持计算段落。如果一个段落没有字符,它将返回数字零,并且每个段落的增量更高。例如,输入文件为:Input,输出应该出现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()

2 个答案:

答案 0 :(得分:2)

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()

答案 1 :(得分:2)

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

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