Python Count段落

2024-04-25 07:57:38 发布

您现在位置:Python中文网/ 问答频道 /正文

大家好,我被派去数行和段落。数每一行显然很容易,但我坚持数段落。如果一个段落没有字符,它将返回数字0,并且每个段落都是一个更高的增量。例如,输入文件是: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()

Tags: incloselenif数字openout字符