如何在python中计算空行数

xt0899hw  于 2021-08-20  发布在  Java
关注(0)|答案(2)|浏览(350)

我正在阅读一个包含空行和单词的文件,例如“car”和“v3hicl3”。我只想将合法的单词写入另一个文件。为此,我希望删除空行和有错误的单词(此处包含数字)。我还想数一数有多少行被阅读、删除和接受。我在捕获空行时遇到问题。在我的列表中,我有:car,v3hicl3,“empty”。我数不清空行。尝试了isspace和line==“\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

欢迎您的任何意见。

ih99xse1

ih99xse11#

你在增加 line_num 对于空行和非空行。你应该是递增的 empty_lines 什么时候 len(line) == 0 .
自从 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
hgb9j2n6

hgb9j2n62#

什么时候 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

相关问题