【问题标题】:Best way to count the number of deleted lines from a file using python使用python计算文件中删除行数的最佳方法
【发布时间】:2021-12-21 19:45:13
【问题描述】:

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

在下面的示例文件中,我有一个 before 用户输入文件和一个 after 文件,用于从用户输入中排除任何包含负数或空格的行。我意识到我要么需要计算之前和之后的文件,要么找到一种方法来计算 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()

【问题讨论】:

  • 您想提高性能还是想缩短或提高可读性?

标签: python indexing count io


【解决方案1】:

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

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

with open(&lt;filename&gt;, &lt;mode&gt;) 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多