蟒蛇计数段落



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

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

最新更新