使用 python 计算文件中已删除行数的最佳方法

Best way to count the number of deleted lines from a file using python

我想知道使用 python 计算文件中已删除行数的最简单方法是什么。是取前后行的索引并相减吗?或者有没有办法计算循环中删除的行数?

在下面的示例文件中,我有一个用户输入之前的文件和一个之后的文件,它们被写入以排除用户输入中具有负数或空格的任何行。我意识到我要么需要计算文件前后的数量,要么找到一种方法来计算 note_consider[] 列表中的项目。

import os, sys

inFile = sys.argv[1]
baseN = os.path.basename(inFile)
outFile = 'c:/example.txt'

#if path exists, read and write file
if os.path.exists(inFile):
    inf = open(inFile,'r')
    outf = open(outFile,'w')

    #reading and writing header
    header = inf.readline()
    outf.write(header)

    not_consider = []

    lines = inf.read().splitlines()
    for i in range(0,len(lines)):
        data = lines[i].split("\t")

        for j in range(0,len(data)):
            if (data[j] == '' or float(data[j]) < 0):
                #if line is having blank or negtive value
                # append i value to the not_consider list
                not_consider.append(i)
    for i in range(0,len(lines)):
        #if i is in not_consider list, don't write to out file
        if i not in not_consider:
            outf.write(lines[i])
            print(lines[i])
            outf.write("\n")   
    inf.close()
    outf.close()

此代码在输入中读取一个文件,并在输出文件中写入非空行或数字。那是您所期望的吗?

如果您不使用not_considered行的信息,那么您可以删除相关代码,并将for line_idx, line in enumerate(ifile.readlines()):替换为for line in ifile.readlines():

with open(<filename>, <mode>) as file: 语句负责在语句范围内打开和关闭文件。

def is_number(line: str):
    try:
        float(line)
        return True
    except ValueError:
        return False


with open("./file.txt", "r") as ifile, open("output.txt", "w") as ofile:
    not_considered = []

    for line_idx, line in enumerate(ifile.readlines()):
        if line == "\n" or is_number(line):
            not_considered.append(line_idx)
            continue

        ofile.write(line)

print(f"not considered  : {not_considered}")
print(f"n not considered: {len(not_considered)}")

输入文件:

this is

1234

a

file

with a lot

42
of empty lines

输出文件:

this is
a
file
with a lot
of empty lines

控制台输出:

not considered  : [1, 2, 3, 5, 7, 9, 10]
n not considered: 7