如何计算 python 中的空行

How to count empty lines in python

我正在读取一个包含空行和单词的文件,例如“CAR”和“V3HICL3”。我只想将合法的单词写入另一个文件。为此,我想删除空行和有错误的单词(此处包含数字)。我还想计算阅读、删除和接受了多少行。我在捕获空行时遇到问题。在我的列表中,我有:汽车,v3hicl3,“空”。我没数空行。尝试了 isspace 和行 ==“\n”。似乎不起作用。我如何计算文档的最后一个空行?

import sys
def read():
    file_name = input("Name of the file to be read: ")
    try:
        file = open(file_name, 'r')
        lst = []
        line_num = 0
        accept_line = 0
        reject_line = 0
        empty_line = 0
        for line in file:
            line = line.strip() 
            if (len(line.strip()) == 0):
                line_num += 1
            if (line == "\n"):
                line_num += 1
            if (len(line) != 0):
                line_num += 1
            if (line.isalpha()):
                accept_line += 1
                lst.append(line)
            else:
                reject_line += 1
    print("Read", line_num, "lines")
    print("Rejected", reject_lines, "lines")
    except FileNotFoundError:
        print("open", file_name, "failure.")
        sys.exit(0)
    file.close()
    return lst, accept_line

感谢任何意见。

您正在为空行和非空行递增 line_num。当 len(line) == 0.

时,您应该递增 empty_lines

因为 line_num 应该计算所有行,在任何条件之外增加它。

import sys

def read():
    file_name = input("Name of the file to be read: ")
    try:
        with open(file_name, 'r') as file:
            lst = []
            line_num = 0
            accept_line = 0
            reject_line = 0
            empty_line = 0
            for line in file:
                line_num += 1
                line = line.strip() 
                if (len(line) == 0):
                    empty_line += 1
                elif (line.isalpha()):
                    accept_line += 1
                    lst.append(line)
                else:
                    reject_line += 1

        print("Read", line_num, "lines")
        print("Rejected", reject_lines, "lines")
        print("Empty", empty_line, "lines")
    except FileNotFoundError:
        print("open", file_name, "failure.")
        sys.exit(0)
    return lst, accept_line

len(line.strip()) == 0 您还必须增加 empty_line

你可以用这个

 import sys
    def read():
        file_name = input("Name of the file to be read: ")
        try:
            file = open(file_name, 'r')
            lst = []
            line_num = 0
            accept_line = 0
            reject_line = 0
            empty_line = 0
            for line in file:
                line = line.strip() 
                if (len(line.strip()) == 0):
                    line_num += 1
                    empty_line += 1
                if (line == "\n"):
                    line_num += 1
                if (len(line) != 0):
                    line_num += 1
                if (line.isalpha()):
                    accept_line += 1
                    lst.append(line)
                else:
                    reject_line += 1
            print("Read {} lines".format(line_num))
            print("Rejected {} lines".format(reject_line))
        except FileNotFoundError:
            print("open", file_name, "failure.")
            sys.exit(0)
        file.close()
        return lst, accept_line